2

In the windows8 Developer preview we can use this code to play audio in background:

mediaElement.AudioCategory = AudioCategory.Media;

In the windows8 Customer perview, It seems that we should use AudioCategory.BackgroundCapableMedia instead of AudioCategory.Media

mediaElement.AudioCategory=AudioCategory.BackgroundCapableMedia;

and I also Declare a background task in appxmanifest

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

but it didn't work for me and the MediaElement will throw an "MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED“ exception in MediaFailed EventHandler How should I do?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
troyou
  • 190
  • 1
  • 13

1 Answers1

3

You also need to set up 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 that should have it working.

bdls
  • 4,578
  • 6
  • 24
  • 30
  • For Windows 8.1+, use `SystemMediaTransportControls`, see [here](http://stackoverflow.com/questions/28249916/how-to-switch-from-mediaelement-to-backgroundmediaplayer-on-application-lose-foc/28250692#28250692). – kiewic Sep 19 '15 at 21:25