Windows Phone allows you to create
apps that respond to the Universal Volume Control (UVC) on the phone
and, in conjunction with an onscreen menu, coordinate the playback of
audio, even when your app is not running in the foreground.
1. Background Agent Recap
Windows Phone supports the following three types of BackgroundAgents
:
- ScheduledTaskAgent
- AudioPlayerAgent
- AudioStreamingAgent
Background agents are
classes invoked by the OS at certain times to carry out activities in
the background (from another process) while your app may not be running
in the foreground.
2. Background Audio Overview
The following compose the three main classes used for background audio:
- BackgroundAudioPlayer
- AudioPlayerAgent
- AudioStreamingAgent
BackgroundAudioPlayer
is a class that allows you to specify an audio file for playback and to
carry out playback activities such as play, pause, fast-forward, and
rewind. BackgroundAudioPlayer
allows you to specify track information, such as the title of the track
and its location. The built-in playback capabilities of the BackgroundAudioPlayer
allow audio files to be streamed from either a remote server, or locally, from isolated storage.
Out of the box, BackgroundAudioPlayer
supports playback formats such as MP3, WMA, and WAV. For a complete list of supported audio formats, see http://bit.ly/pnnT8C. BackgroundAudioPlayer
offers little in the way of coordinating the playback of a list of audio tracks. This is the task of the AudioPlayerAgent
.
AudioPlayerAgent
classes are specialized agents used to retrieve audio files and coordinate the playback of background audio via the BackgroundAudioPlayer
. AudioPlayerAgent
provides a number of method callbacks that are automatically called by the OS based on user actions.
An AudioStreamingAgent
can be used to customize how audio data is procured by your BackgroundAudioPlayer
. You can supplant the BackgroundAudioPlayer
’s media sourcing capabilities by providing your own logic to download and decode an audio file.
3. Background Audio Player
The BackgroundAudioPlayer
class implements the singleton pattern. The single instance of the BackgroundAudioPlayer
is accessed in your foreground app via its static Instance
property, as shown:
BackgroundAudioPlayer.Instance.Track
= new AudioTrack(
new Uri("http://example.com/Audio.mp3", UriKind.Absolute),
"Track Name", "Artist", "Album", null);
BackgroundAudioPlayer.Instance.Play();
The behavior of the BackgroundAudioPlayer
changes depending on whether it is being used in your foreground app or from an AudioPlayerAgent
. When the BackgroundAudioPlayer
is used in your foreground app, all calls are relayed to your AudioPlayerAgent
. When used from an AudioPlayerAgent
, the BackgroundAudioPlayer
affects the playback of audio directly. Without an AudioPlayerAgent
, the BackgroundAudioPlayer
does nothing.
This dual behavior can be best understood by looking at the internal workings of the BackgroundAudioPlayer
. The BackgroundAudioPlayer
is a proxy that relies on a native implementation of an internal IAudioPlaybackManager
interface. When the BackgroundAudioPlayer
is used in your foreground app, the IAudioPlaybackManager
relays all audio requests to the app’s AudioPlayerAgent
. Conversely, when used from your AudioPlayerAgent
, the IAudioPlaybackManager
uses the native audio playback system instead.
BackgroundAudioPlayer
contains several public methods that allow you to control the playback of audio (see Table 1).
TABLE 1. BackgroundAudioPlayer
Audio Playback Related Methods
BackgroundAudioPlayer
contains several public audio playback-related properties, which are shown in Table 2.
TABLE 2. BackgroundAudioPlayer
Audio Playback-Related Properties
BackgroundAudioPlayer
contains a principle event, PlayStateChanged
, which is used to detect, for example, when a track begins playing.
New in Windows Phone 8 is the capability to retrieve the current play state and preceding play state from the PlayStateChangedEventArgs
. For compatibility with Windows Phone 7.1 OS, the signature of the PlayStateChanged
event has not changed in the Windows Phone SDK. Therefore, to retrieve the play states, you must cast the EventArgs
argument to a PlayStateChangedEventArgs
object, as shown in the following example:
void HandlePlayStateChanged(object sender, EventArgs e)
{
/* New in Windows Phone 8, the current and previous playstate
* can be retrieved using the PlayStateChangedEventArgs. */
PlayStateChangedEventArgs args = (PlayStateChangedEventArgs)e;
PlayState currentPlayState = args.CurrentPlayState;
PlayState previousPlayState = args.IntermediatePlayState;
...
}