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

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:

  1. The user is currently playing music
  2. 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.
Advertisements

About Ben Kane
I'm a game developer with a heart of gold.

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

  1. RRF says:

    I’m currently developing a game for Windows Phone 7, but we are having problems playing back music because of GameHasControl equals false. We ask the user for permission to stop music playback and play our music instead, but then what? We can’t just say GameHasControl = true and do what we want to.
    Could you explain what you mean with “If the user set this to true, I would re-enable my audio system and start playing music.”? We’ve had no luck at all finding how to gain control of audio playback so we can stop the user’s music and play our own. Any info is very much appreciated!

    • Ben Kane says:

      GameHasControl is just used to check if the user is currently playing music. You should be able to call MediaPlayer.Play(song) regardless of the value of GameHasControl. That is to say, you are always capable of overriding the user’s preference and playing whatever music you want – however you will fail certification if you do so without obtaining the user’s permission.

      It sounds like you’re saying that you are unable to play music at all though. Are you using a physical device connected to a PC with Zune running? In that case, you’ll end up with an exception (which you might be silently catching) and thus no music will play. See this for more info: http://gamedev.stackexchange.com/questions/7823/playing-a-song-causing-wp7-to-crash-on-phone-but-not-on-emulator

  2. Zach Hoefler says:

    Thank you very much! I thankfully had all my sound/music calls going through an AudioManager class, so I could just check e.g. “if (MusicEnabled && MediaPlayer.GameHasControl)”. Seems to work fine, and it was a quick fix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: