Passing Windows Phone Certification – Correctly Working with Media

28 במרץ 2012

Tip #1: How not to break Windows Phone certification rule No. 6.5 – Correctly working with the Media Player.

Oh this is a fun one…
The first certification rejection I ever got was because of this one. It's a very simple rule, but not knowing of it's existence or how to abide it in code can make things tricky when you try to fix it later.

The idea is fairly basic, if the user has music playing while entering your application (via Zune or whatnot), you can't hijack the Media Player and play what you want without asking for permission first.
This is fairly logical for several reasons. For one, if you build a game, the user might want to play it while having his own music in the background.
Another scenario is this one: The user, whilst having music playing, enters your application, suddenly the music stops and the user doesn't know why, enraged with confusion the user throws his phone to the ground and breaks it.
All in all this rule is pretty effective in maintaining UX in all that regards playing music in the background, a very common use case.

What does it mean for you as a developer?
Before using any class that plays music using the media player (
Song, SongCollection, MediaElement, etc.), you must check if music is already playing, and if so you must ask the user for permission before playing anything.

 

But how do I know if the user is playing music?
Well this is where it gets a bit ugly; The only way you can know for sure is using the
Microsoft.Xna.Framework.Media.MediaPlayer class.

Yes, XNA. Sorry all you Silverlight purists, but like in a lot of things on the WP developer’s platform, you have to use XNA libraries for certain things.

The MediaPlayer class contains a property called GameHasControl, this kinda confusingly named property actually indicates whether someone else is already using the media player, like the Zune player for example. If no one is using the media player, this means your application has "control" over it and consequently the property will be True, otherwise it will be False.

What this means is that before doing anything related to the media player you must first check this property, and if it is False (meaning you don't have control) ask the user for permission.
Translated into simple code, it will look like this:

    HasPermission = true;

 

    if (!MediaPlayer.GameHasControl)

    {

        if (MessageBox.Show("Can I please stop the music?", "Music Already  Playing", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)

         {

             HasPermission = false;

         }

     }

 

     if (HasPermission)

     {

        // Load and play music or navigate to a page that will

     }

When do you ask for permission and what you do if you don't get it is obviously up to you. This decision should be made considering the design and flow of your application.

Just keep in mind these few things:

1) Never prompt the user before the first page has loaded. It's just not user friendly.

2) Consider what happens if the user doesn't want to stop his music at the moment, but later he does.

3) If you are building a game or even an application, if the music is not essential don't make the user give his permission to stop the playing music in order to continue. He might want to play while having his own music playing, so give him the option.

4) If you are creating a MediaElement from XAML, note that assigning it a source, will cause loading the page to stop the currently playing music (because you load a new song to the media player, your MediaElement's song).

5) If your application or game plays background music throughout it's usage, you must include an option to mute and/or change the volume of the playing music (requirement 6.5.2).

More posts about passing certification coming soon.
If you have specific trouble with certification you wish me to write about, please leave a comment bellow.

Josef.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. SHD29 במרץ 2012 ב 21:53

    I actually just got an application rejected from the store because of that rule.
    it was really simple following your instruction and now i'm going to try uploading the app again. thanks…

    Reply
  2. Alex T24 במאי 2012 ב 22:20

    Shalom Josef,

    Well, one of the most recent pain points in the certification process (not only for me, as I had a chance to discover) is the notorious Section 3.10 compliance (suggestive images, alcohol references etc. when publishing in certain countries).
    In my case, the app ("ShowMe") is used for tracking the TV series you watch, with the "crowd-sourced" TheTVDB.com as its data provider.
    I had the misfortune of clicking the "Select All Countries" checkbox while submitting a periodic update… Which resulted in certification failure – since the tester managed to find a show whose banner was considered suggestive (not a part of my app's inherent content, mind you).
    Frankly, I don't know what the right strategy is for me to deal with such BS.
    A *normal* way would've been for MS to allow showing some sort of "This app contains XYZ content, do you want to proceed anyway?" popup when it's being installed (similarly to how iOS handles this), or when it's run first time. But that, alas, is not how the certification requirements are formulated…
    So, for now I just uncheck the "problematic" countries (China + the Muslim block) – to be on the safe side.
    I am not quite sure that was Microsoft's intention…

    Reply