How to Play Music in XNA games on Windows Phone 7 (and still pass Cert)

November 23, 2010

I’ve seen this come up in the forums a few times now: how do you properly handle music in an XNA game for Windows Phone 7? I got snagged by this in certification too, so I have some experience on how to handle it. Note that this is just my experience and there are always other ways to do things. This applies to XNA games on WP7.

What You Need To Consider

WP7 users can play music while playing your game. If your game plays music, you can’t simply interrupt the user’s current music. The relevant section in the Certification Requirements is “6.5 Applications that Play Media”.

6.5.1 Initial Launch Functionality

When the user is already playing music on the phone when the application is launched, the application must not pause, resume, or stop the active music in the phone MediaQueue by calling the Microsoft.Xna.Framework.Media.MediaPlayer class.

If the application plays its own background music or adjusts background music volume, it must ask the user for consent to stop playing/adjust the background music (e.g. message dialog or settings menu). 6.5.2 Configurable Functionality

If the application needs to play its own background music or adjust background music volume, it must provide the user with the configurable settings for its music.

The Basic Plan

Music in an XNA game should be played using the MediaPlayer class. If you use this class, you will automatically avoid the case where your music is playing at the same time as the user’s music. Note that if you try to use the SoundEffect class to play music, both your music and the user’s music can play simultaneously. This will fail certification and is specifically disallowed in the requirements.

Before we play music, however, we need to check if:

The user is currently playing music We have permission to override the user’s music

The first case is simple. All we need to do is check if

MediaPlayer.GameHasControl

is true. If it is, then the user is not playing music and we have free reign to do what we want.

If the game does not have control though, we can’t call MediaPlayer methods (like Play, Stop, or Volume). At least, not unless the user says it’s okay. There are a few ways to approach this. We could prompt the user for permission when the game starts using something like a message box. That would work, but I would personally find that pretty intrusive.

Instead, I opted to have an option called “Allow game to override active music” in my options menu. If the user set this to true, I would re-enable my audio system and start playing music. Since it was part of the saved user settings, the game remembered the user’s choice for later.

Further Exceptions

If your game uses music for a cutscene, you can get away with interrupting the user’s music provided you resume it afterward. See section 6.5.3:

6.5.3 Applications that Play a Video or Audio Segment

An application may interrupt the currently playing music to play a non-interactive full motion video or audio segment (e.g. cut-scene or media clip) without asking for user consent. If music was playing prior to the segment, the application must resume music when the segment has completed.

Things To Remember

Don’t use SoundEffect to play music

Don’t change anything using MediaPlayer unless GameHasControl is true, or you have asked the user for permission in some way

Don’t simply check (MediaPlayer.State != MediaState.Playing) to see if you can play music. Use MediaPlayer.GameHasControl.

Perform your checks when resuming from tombstone, not just at launch. A user can tombstone, start music and then return to your game.