Wednesday, December 29, 2010

XNA Game Development Tips and Tricks 3: Windows Phone 7 Playing Music and Sound FX

You are developing a game, and your game is pretty decent but game without sound and music is not good game and does not create excitement that it needs to. For example Xbox 360 has Achievement earning, when you earn achievement sound of achievement earning is so nice to ears that you would love to play game just for the sake of earning achievements. Anyways that's apart coming back to todays tips and tricks. We are going to see how you can play Sound and Music in game using Windows Phone 7, what are challenges, tips and tricks.

XNA provides two ways of playing sound/music:

1 – SoundEffect
2 – Song using MediaPlayer

When you want to play small sounds, like explosion of bomb or sound of coin dropping on floor or similar sound effects you would want to use SoundEffect class. and when you want to play Background Music you would want to use Song class. Difference is that SoundEffect only plays WAV files, so when you use Music as WAV file, your music would be at-least 1 minute long, and 1 minute for WAV file is huge, your wav file will be very large approximate 20-30 MB.

Song class has another issue that at a time we can play only one file, we can not play more than 1 file simultaneously. So if you want to play Human voice, you might want to make them as WAV file and play them using SoundEffect class.

Have a look at following code:

SoundEffect sfx = Content.Load<SoundEffect>(“coinsound”);
sfx.CreateInstance().Play();

Above code will load content which is “coinsound” in wav file format, we create it’s instance and then play. Now you must be wondering why to call CreateInstance? when we are already initializing object. Now take a case when you collect coin you want to play sound so you execute above code, now above code works fine, but what if we collect multiple coins at same time then what to do in that case? In that case above code will produce sound of coin only once, even if you execute Play method multiple times, and will not give kind of effect we are looking for. So to produce multiple sound effect of same sound file, we can use same object but every time we will have to call CreateInstance and then call Play method.

Now, for playing Music we write below code:

Song sng = Content.Load<Song>(“backmusic”);
MediaPlayer.Play(sng);
MediaPlayer.IsRepeating = true;

Notice we are using MediaPlayer class for playing music, it means it will be using Zune Player to play the music. Now when you write above code and run your application you will come across given below error:

“Song playback failed. Please verify that the song is not DRM protected. DRM protected songs are not supported for creator games.”

image

This happens because your Windows Phone’s Zune Library is locked by Zune Software on PC.. Currently possible way of doing is to first run your game through Visual Studio on Device, when you encounter above error, stop debugging, disconnect device from PC and then start game on device through applications, and now you should be able to hear Music. NOTE: When you do SoundEffect you will not have any problem as above, It is only with MediaPlayer.

No comments:

Post a Comment