Windows Phone 8 and the music resume -rules in the marketplace

Seems that this old issue is still causing some scratching of head among the devs, how to implement this properly in Windows Phone. This has changed a bit from WP7, and the rule has been updated (6.5.1. & 6.5.3) to apply only to XAML apps. Now you don’t have to worry about radio anymore, as that’s not in the mix with WP8.0.

Here are the few scenarios where you need to fill this requirement.

First we implement the code for normal apps, which want to play a non-interactive full motion video, or a non-interactive audio segment (e.g. cut-scene or media clip) – in this scenario you don’t need to ask user’s consent for this action.

Here are the steps needed to implement it properly. Open your MainPage.xaml.cs file, and insert these namespaces,  and after that also insert them to your App.xaml.cs as well:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Media;

And then add a boolean variable to your App.xaml.cs to mark if resume is needed:

public bool needToResumeMusic = false;

Then you go to the class where you need to play your own media. First add this code as a class level variable:

App thisApp = (App)Application.Current;

Next you open the method where you are going to play your own media, and add this code before it:

FrameworkDispatcher.Update();

if (!MediaPlayer.GameHasControl)
{
    thisApp.needToResumeMusic = true;
    MediaPlayer.Pause();
}

Please note that the Update is critical to be there, or the app will have serious stability issues.

Then you need to put only the resume part to your code, and if you’re using for example MediaElement to play music/video, it has MediaEnded -event which you implement like this:

void Player_MediaEnded(object sender, RoutedEventArgs e)
{
      // Do your cleanup here
      ...
      if (thisApp.needToResumeMusic)
      {
         MediaPlayer.Resume();
      }
}

Now it can handle the switching of background music, but there are two scenarios which you still would want to handle. User might navigate away or kill your app while you’re in the process of playing your music, and you would want to resume the background music on those cases as well. That’s why you edit your App.xaml.cs with these changes:

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    if (needToResumeMusic)
    {
       MediaPlayer.Resume();
    }
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
   if (needToResumeMusic)
   {
      MediaPlayer.Resume();
   }
}

Second scenario is that you want to play a background music in your own app, in which you would just add dialog box asking for consent just before the GameHasControl -check and move the Resume to the exit of your app.

So, it’s pretty simple to tackle this nasty little rule without too much coding. Hope it clears things and helps you pass the store certification!

Music Resume Sample – includes code, just put your own media file there and change the name of the file to play to test this.

About Jani Nevalainen

Windows developer who's been working on Microsoft Platforms since 1996. Windows Phone dev MVP 2013, Windows Development Platform MVP 2014. Currently working as Technical Evangelist for Microsoft Finland Developer Experience team.
This entry was posted in Windows Phone, Windows Phone 8 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *