MediaPlayer class

I checked some things about the submission process lately, and I discovered the way I played musics would just help me to fail the certification…

I used to play music that way (not my actual code, it’s just an example) :

SoundEffect soundEffect = content.Load<SoundEffect>("Sounds/mySong");
SoundEffectInstance songInstance = soundEffect.CreateInstance();
songInstance.IsLooped = true;
songInstance.Play();

Well, it worked fine, and I guess using it to play songs on PC or Xbox 360 is not a problem… But the issue is, on a Windows Phone, if the user is playing his own music it will just play both the user’s music and the game music at the same time. And looks like it’s not allowed.

So I read we should use instead the MediaPlayer class to check if the game has the control or not (which means if the user is using the media player or not…). But we still can prompt the user when the application is launching to ask if he would like to hear the game’s music instead of the one he’s listening. Then, I changed it into something like :

if (!MediaPlayer.GameHasControl)
{
   IAsyncResult guide = Guide.BeginShowMessageBox(
   "Warning", "Do you want to play the game's music instead  
   of the one you are listnening ?", new string[] { "YES", "NO" }, 0,
   MessageBoxIcon.Warning, null, null);
   int result = Guide.EndShowMessageBox(guide);
   if (result == 0) // YES    
   {
        Song songInstance = content.Load<Song>("Sounds/mySong");
        MediaPlayer.Play(songInstance);
   }
   else // NO    
   {
   }
}


The other thing is with the MediaPlayer class we can play mp3 songs... I could only play wav with the SoundEffect... Cool then. That's an important improvement !

Source : http://benkane.wordpress.com/2010/11/23/how-to-play-music-in-xna-games-on-windows-phone-7-and-still-pass-cert/
Advertisements

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