App Hub
Banner of Game Development Tutorial

playing sounds

Add sounds and music to make your game more immersive

 

We have made a fair bit of progress in our game just focusing on input and graphics; while they are key components of gameplay, an often-unsung hero is just as important: sound. The right sound effects and music can make your game exceptional, and without it, players might find something lacking that they can’t quite put their finger on.

We have a few in-game events that would do well to have some sound effects, and a good beat is always helpful to make a shoot-em-up game feel right. Let’s get sounds and music into our game. We’ll be working entirely within Game1.cs for this.

Switch to the Game1 class by double-clicking the Game1.cs file in the Solution Explorer in Visual Studio.

inside game1.cs

There are two classes that we need to make objects out of – SoundEffect and Song. They’re pretty self-explanatory, and load up very similar to the graphical assets we use. The first thing is to declare them up at the top of our class with everything else.

Look for the first { mark under the start of the Game1 class, and go just below List<Animation> explosions; that you added in the previous step. Add a new line and then add the following:

// The sound that is played when a laser is fired
SoundEffect laserSound;

// The sound used when the player or an enemy dies
SoundEffect explosionSound;

// The music played during gameplay
Song gameplayMusic;

Now, since it’s all content, we’ll load up these objects inside the LoadContent() method. One note – not only will we load up the items, but we’ll start playback of the Song item right inside the LoadContent() method.

Find the LoadContent() method. Inside that method, below the explosionTexture = call, add these lines:

// Load the music
gameplayMusic = Content.Load<Song>("sound/gameMusic");

// Load the laser and explosion sound effect
laserSound = Content.Load<SoundEffect>("sound/laserFire");
explosionSound = Content.Load<SoundEffect>("sound/explosion");

// Start the music right away
PlayMusic(gameplayMusic);

You’ll notice that we’re calling PlayMusic(). If you were to try to build right now you’d get a bit of a surprise – PlayMusic() doesn’t exist! We need to make this method, but it’s easy. Find some empty space inside the Game1 class that’s not in the middle of any other method. Add a new line then add the following:

private void PlayMusic(Song song)
{
// Due to the way the MediaPlayer plays music,
// we have to catch the exception. Music will play when the game is not tethered
try
{
// Play the music
MediaPlayer.Play(song);

// Loop the currently playing song
MediaPlayer.IsRepeating = true;
}
catch { }
}

This method performs a bit of a trick in order to help this game play equally well on the Phone emulator as well as the Phone device. At the time of this writing, a plugged-in device will raise an exception (a notification in the code that can be intercepted and dealt with) that we’d rather not have raised. We deal with it here.

Now that we’re playing music, let’s deal with our sound effects. We have two loaded – a laser and an explosion. There’s a perfect place for each of these. First, the laser should go in UpdatePlayer(), right under the call to AddProjectile(), so when the player fires a projectile, a sound is immediately played.

Scroll down and find the UpdatePlayer() method. Look for the line marked AddProjectile(player.Position + new Vector2(player.Width / 2, 0); just below it, add the following lines:

// Play the laser sound
laserSound.Play();

We’ll make explosions sound good, too. Explosions happen inside the UpdateEnemies() method when enemies’ health goes down to zero. We’ll put our sound effect call there.

Scroll down and find the UpdateEnemies() method. Look for the line marked AddExplosion(enemies[i].Position); just below it, add the following lines:

// Play the explosion sound
explosionSound.Play();

You’ve done it! Sound effects and music are ready to play as soon as you build and run the game.

running the game

Build and run your game by pressing CTRL+F5, to hear the sound effects and music at work. You should be hearing a laser every time the player fires, an explosion every time an enemy is destroyed, and there should be music playing in the background.

If you’re not hearing sound effects, check your code against the code above to be sure you’ve got it all. If you’re not hearing music, please note that you won’t hear any music if you’re on a physical Windows Phone 7 device that’s still tethered to your computer. If you untether your device and run the game from the menu on the phone, you should hear music again.

We’re off to our final coding challenge; in the next section, we’re going to add a user interface to the game that displays the player’s health and score.

Move on to the next step: Adding Score and Health

var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG