Introduced
Special media for Windows Phone 7.5 (SDK 7.1)
Play audio in the background with audioplayeragent implementation
Example
Demonstrates how to perform audio playback in the background through a background proxy
1. Background Agent
Myaudioplayeragent/audioplayer.cs
* * This example shows how to play background audio (in audioplayeragent as an example, another audiostreamingagent for streaming audio) * It is recommended to create this project using audioplaybackagent type of template * * Backgroundagent-Background proxy class, abstract class, which is the base class for Scheduledtaskagent, Audioplayeragent, and Audiostreamingagent * Notifycomplete ()- To the notification system, the agent has completed the current task, calling this method before the system prepares to perform the next task * Abort ()-To notify the system, discard this and future tasks * ONCANCEL ()-the method that is executed when the background agent is canceled (called by the system, such as after This method is called before the agent can go to hibernation or terminate state () * * Audioplayeragent-the proxy class for background playback audio * ONERROR ()-this method is called when an error occurs in playback * Onplaysta Techanged ()-When the playback state changes, the system calls this method (except for the error state) * Onuseraction ()-The system calls this method * Playstate-microsoft when the program changes playback behavior. Phone.BackgroundAudio.PlayState enum * Unknown, Stopped, paused, Playing, bufferingstarted, bufferingstopped, Trackrea DY, trackended, rewinding, fastforwarding, Shutdown, Error * * Useraction-microsoft.phone.backgroundaudio.usera Ction enumeration * Stop, Pause, play, Skipnext, skipprevious, Fastforward, rewind, Seek * audiotrack-Audio Objects * Source-Audio address, remote address or singleStorage address can be * Title-Audio name * Duration-Audio Length * Album-Album name * Albumart-Album cover page Uri address, remote address or isolated storage address can be, if Using isolated storage addresses, the directory must be displayed by the player control (Microsoft.Phone.BackgroundAudio) in the state of the Shared/media * Artist-artist * playercontrols-lock screen. Enabledplayercontrols enum) * None = 0, Skipnext = 1, skipprevious = 2, Fastforward = 4, rewind = 8, Pause = LL = * Tag-arbitrary content, that is, audiotrack binding a context data * BeginEdit ()-set Audiotrack to edit state (if you need to modify the properties of the audiotrack you will need to change the Au
Diotrack set to edit state) * EndEdit ()-End of Audiotrack edit state * * using System;
Using System.Windows;
Using Microsoft.Phone.BackgroundAudio;
Using System.Collections.Generic; Namespace Myaudioplayeragent {public class Audioplayer:audioplayeragent {//Play list private static list<audiotrack> _playlist = new List<audiotrack> {new Audiotrack (new Uri ("Super Mario.mp3 ", urikind.relative)," title "," Artist "," album ", NULL, NULL, ENABLEDPLAyercontrols.pause), New Audiotrack (The new Uri ("Http://traffic.libsyn.com/wpradio/WPRadio_29.mp3", Urikind.abs
Olute), "title", "Artist", "album", NULL, NULL, enabledplayercontrols.all};
The position of the currently playing Track in the entire playlist private static int _currenttracknumber = 0; * * _classinitialized-Used to mark whether the Audioplayer has been initialized * marked volatile to avoid the compiler's belief that this field is not externally modified, and that it is optimized into registers (tag Volatile fields are placed in memory only) * Generally speaking, fields shared between tasks in a multitasking environment should be marked as volatile * * private static volatile Bo
OL _classinitialized; Public Audioplayer () {if (!_classinitialized) {_classinitialized = t
Rue Deployment.Current.Dispatcher.BeginInvoke (Delegate {Application.Current.UnhandledEx
Ception + = audioplayer_unhandledexception;
}); } private void Audioplayer_unhandledexception (obJect sender, Applicationunhandledexceptioneventargs e) {if (System.Diagnostics.Debugger.IsAttached)
{System.Diagnostics.Debugger.Break (); The method that is called when the playback state changes protected override void Onplaystatechanged (Backgroundaudioplayer pla Yer, audiotrack track, playstate playstate) {switch (playstate) {CAs E PlayState.TrackReady:player.
Play ();
Break
Case PlayState.TrackEnded:PlayNextTrack (player);
Break
Case PlayState.Playing:break;
Case PlayState.Paused:break;
Case PlayState.Stopped:break;
Case PlayState.BufferingStarted:break;
Case playstate.bufferingstopped: Break
Case PlayState.Rewinding:break;
Case PlayState.FastForwarding:break;
Case PlayState.Shutdown:break;
Case PlayState.Unknown:break;
} notifycomplete (); //The method invoked when the program changes playback behavior protected override void Onuseraction (Backgroundaudioplayer player, Audiotrack Track, useraction action, object param) {switch (action) {case UserA Ction.
Play:playtrack (player);
Break
Case UserAction.SkipPrevious:PlayPreviousTrack (player);
Break
Case UserAction.SkipNext:PlayNextTrack (player);
Break Case UserAction.Rewind:player. Rewind ();
Break Case UserAction.FastForward:player.
Fastforward ();
Break Case UserAction.Seek:player.
Position = (TimeSpan) param;
Break Case UserAction.Pause:player.
Pause ();
Break Case UserAction.Stop:player.
Stop ();
Break
} notifycomplete (); //play next Track private void Playnexttrack (Backgroundaudioplayer player) {if
(++_currenttracknumber >= _playlist.count) _currenttracknumber = 0;
Playtrack (player);
//Play last Track private void Playprevioustrack (Backgroundaudioplayer player) { if (--_currenttracknumber < 0) _currenttracknumber = _playlist.count-1;
Playtrack (player); //Play the current Track private void Playtrack (Backgroundaudioplayer player) {player.
Track = _playlist[_currenttracknumber]; /////The method invoked when an exception occurred protected override void OnError (Backgroundaudioplayer player, audiotrack track, exc
Eption error, bool isfatal) {if (isfatal) Abort ();
else Notifycomplete ();
The system automatically calls this method before the background agent is transferred to hibernation or terminates, and needs to be completed within 5 seconds protected override void OnCancel () { When the main program references this project, the following information is added to the manifest: * <extendedtask name= "Backgroundtask" > * <backgr Oundserviceagent specifier= "audioplayeragent" name= "myaudioplayeragent" source= "Myaudioplayeragent" Myaudioplayeragent.audioplayer "/> * </ExtendedTask> *