Playing audio in the background using BackgroundCapableMedia

In a preview of Windows 8 Developer, we can use this code to play audio in the background:

mediaElement.AudioCategory = AudioCategory.Media; 

In the Windows8 client view, it seems that we should use AudioCategory.BackgroundCapableMedia instead of AudioCategory.Media

 mediaElement.AudioCategory=AudioCategory.BackgroundCapableMedia; 

and also declare a background job in appxmanifest

 <Extension Category="windows.backgroundTasks" EntryPoint="TestApp.App"> <BackgroundTasks> <Task Type="audio" /> </BackgroundTasks> </Extension> 

but this did not work for me, and MediaElement will throw an exception "MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED" in MediaFailed EventHandler How can I do this?

+2
source share
1 answer

You also need to configure these event handlers:

using Windows.Media;

 MediaControl.PlayPressed += MediaControl_PlayPressed; MediaControl.PausePressed += MediaControl_PausePressed; MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed; MediaControl.StopPressed += MediaControl_StopPressed; 

-

 void MediaControl_StopPressed(object sender, object e) { myMediaPlayer.Stop(); } void MediaControl_PlayPauseTogglePressed(object sender, object e) { } void MediaControl_PausePressed(object sender, object e) { myMediaPlayer.Pause(); } void MediaControl_PlayPressed(object sender, object e) { myMediaPlayer.Play(); } 

I think it should work.

+3
source

Source: https://habr.com/ru/post/944042/


All Articles