Original: Windows Phone 8.1 Multi Media (3): Music
Windows Phone 8.1 Multi Media (1): Photos
Windows Phone 8.1 Multi Media (2): Video
Windows Phone 8.1 Multi Media (3): Music
(1) Front desk
Said is the front desk, in fact the music broadcast is using the system backstage Backgroundmediaplayer:
BackgroundMediaPlayer.Current.SetUriSource (Thenew Uri ("Ms-appx:///djlang59_-_drops_of_h2o_"(_ The_filtered_water_treatment_). mp3")); BackgroundMediaPlayer.Current.Play ();
You can add music playback status change events:
BackgroundMediaPlayer.Current.CurrentStateChanged + =mediaplayerstatechanged;Private Async voidMediaplayerstatechanged (MediaPlayer sender,Objectargs{awaitDispatcher.runasync (Coredispatcherpriority.normal, () = { Switch(BackgroundMediaPlayer.Current.CurrentState) { CaseMediaPlayerState.Stopped:AppBarBtnPause.IsEnabled=false; Appbarbtnstop.isenabled=false; Break; CaseMediaPlayerState.Playing:AppBarBtnPause.IsEnabled=true; Appbarbtnstop.isenabled=true; Backgroundmediaplayer.sendmessagetobackground (NewValueSet {{"Title","Drops of H2O"}, {"Artist","J.lang"}, }); Break; CaseMediaPlayerState.Paused:AppBarBtnPause.IsEnabled=false; Break; } });}
Where the Backgroundmediaplayer.sendmessagetobackground method can send a message to the background music player, you can add processing events to the background program:
Backgroundmediaplayer.messagereceivedfromforeground + =Backgroundmediaplayeronmessagereceivedfromforeground;Private voidBackgroundmediaplayeronmessagereceivedfromforeground (Objectsender, Mediaplayerdatareceivedeventargs e) {Systemmediatransportcontrol. Displayupdater.type =Mediaplaybacktype.music; Systemmediatransportcontrol. DisplayUpdater.MusicProperties.Title= e.data["Title"]. ToString (); Systemmediatransportcontrol. DisplayUpdater.MusicProperties.Artist= e.data["Artist"]. ToString (); Systemmediatransportcontrol. Displayupdater.update ();}
(2) Backstage
The background here refers to the Systemmediatransportcontrols, which is the control that controls the music playback by pressing the volume key and ejecting the top.
1) New Windows Runtime Component and inherited Ibackgroundtask background classes
The A:run method can be used to set the individual key properties of the Systemmediatransportcontrols and the subscription of events (such as the Next button is not available, state change events, etc.)
Public voidRun (ibackgroundtaskinstance taskinstance) {Systemmediatransportcontrol=Systemmediatransportcontrols.getforcurrentview (); Systemmediatransportcontrol. Buttonpressed+=systemcontrolsbuttonpressed; Systemmediatransportcontrol. IsEnabled=true; Systemmediatransportcontrol. Ispauseenabled=true; Systemmediatransportcontrol. Isstopenabled=true; Systemmediatransportcontrol. Isplayenabled=true; BackgroundMediaPlayer.Current.CurrentStateChanged-=backgroundmediaplayercurrentstatechanged; Backgroundmediaplayer.messagereceivedfromforeground-=Backgroundmediaplayeronmessagereceivedfromforeground; BackgroundMediaPlayer.Current.CurrentStateChanged+=backgroundmediaplayercurrentstatechanged; Backgroundmediaplayer.messagereceivedfromforeground+=Backgroundmediaplayeronmessagereceivedfromforeground; Deferral=taskinstance.getdeferral ();}
B: Handling of events
Private voidBackgroundmediaplayercurrentstatechanged (MediaPlayer sender,Objectargs) { if(sender.) CurrentState = =mediaplayerstate.playing) {Systemmediatransportcontrol. Playbackstatus=mediaplaybackstatus.playing; } Else if(sender.) CurrentState = =mediaplayerstate.paused) {Systemmediatransportcontrol. Playbackstatus=mediaplaybackstatus.paused; }}Private Static voidsystemcontrolsbuttonpressed (systemmediatransportcontrols sender, Systemmediatransportcontrolsbuttonpressedeventargs args) {Switch(args. Button) { CaseSystemMediaTransportControlsButton.Play:BackgroundMediaPlayer.Current.Play (); Break; CaseSystemMediaTransportControlsButton.Pause:BackgroundMediaPlayer.Current.Pause (); Break; CaseSystemMediaTransportControlsButton.Stop:BackgroundMediaPlayer.Current.Pause (); BackgroundMediaPlayer.Current.Position= Timespan.fromseconds (0); Break; }}
2) Foreground app add a new background task in the previous step