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 (MessageBox.Show("Can I please stop the music?", "Music Already Playing", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
HasPermission = false;
// 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.