Playing Sound Effects
Xna4.png

Introduction

In this tutorial, we will take a look at how to play sound effects in your game. We will talk more about XACT later, but for now, we are just going to directly play the sound effects in our game. The first step is to acquire the sound effects that you want. If you didn't take a look at my suggestions for sound effects in games, feel free to check it out. At any rate, go find the sound effects that you would like before continuing on to the next paragraph. (Worst case, your computer will already have some on it somewhere that you can play around with.)

Adding Sound Effects to your Game

Adding sound effects to your game project is the exact same as when we add any other kind of content to our games. We add it to the Content node of the Solution Explorer. I usually create a separate folder in my content for the sound effects (usually called "SoundFX"). Add your sound effects like we have done before (usually by right-clicking on the directory that you want to put them in, then choosing Add > Existing Item, and browsing for your sound effects.

Once the sound effects are in your game's content, they will appear in the Solution Explorer and you will be ready to play them. If your sound effects have the extension .wav, XNA Game Studio will automatically configure them to be processed with the WAV Audio File Content Importer and the Sound Effect Content Processor, but you may want to double-check this, just to make sure that it is using the right importer and processor. To do this, select the sound effect file and look at the Properties window. (If the Properties window isn't opened, you can right-click on the file and choose Properties from the popup menu.) In the field that says Content Importer, make sure that it says WAV Audio File (or if you are using an MP3 file or WMA file, choose the MP3 Audio File or WMA Audio File). Right below that, in the Content Processor field, make sure that it says Sound Effect.

Playing Sound Effects in your Game

The first thing you will need is to create a variable to store your sound effects in. In this tutorial, we will create an instance variable in our main game class at the top of the class by the rest of your variable declarations, so add the following code there:

private SoundEffect effect;

You'll want to create a separate SoundEffect object for each of the sounds that you want to play. You do not need to create a new SoundEffect object every time you want to play the sound effect, just the once at the beginning. You can create as many SoundEffects as you want until you run out of memory.

Next, we need to load our SoundEffect objects with the data in the sound effect files. We do this in the same way that we've always loaded content. So in your LoadContent() method, add the following line to read in your sound effects:

effect = Content.Load<SoundEffect>("SoundFX/ExtraLife");

Be sure to change the names to the filenames that you are using. This will load our sound effects so they are ready to be played.

The only thing that is left is to actually play the sound effect. This is done with the following statement:

effect.Play();

You will want to place this wherever the sound should be played in your game. Note that a sound effect can be played multiple times, even before the first time hasn't finished yet. If you just put this in your Update() method in a place that it will be called every time, you'll get a lot of noise. So just place this wherever you need it in your game and you're set!

Now, in addition to that, there's another version of the Play() method that give us more options. The following gives you the option to vary the volume, pitch, and pan of the sound effect (all described more in a second) from the original:

float volume = 1.0f;
float pitch = 0.0f;
float pan = 0.0f;
effect.Play(volume, pitch, pan);

The volume variable can range from 0, which is completely quiet, to 1, which is the full volume (the volume of the original sound effect).

The pitch variable lets you change the pitch of the sound effect. -1 lowers the pitch by an octave, while +1 raises it by one octave. 0 means that it should stay the same as the original (what they call the "unity" pitch). Essentially, this stretches out or shrinks the actual sound wave, which changes the pitch, but it is also important to know that this means it also stretches out the amount of time it takes to play the sound effect. By the way, the full two-octave range gives you a ton of variability. You shouldn't even need that much!

pan is used to basically tell you which speaker the sound should come out of if you are using stereo sound (almost everything is these days). -1 means that the sound will be played in the left speaker only, and be silent in the right speaker. 0 means that it will be played on both speakers. +1 means that it will be played with only the right speaker. Once again, the valid range is anywhere from -1 to +1.

SoundEffectInstances

Before wrapping up our discussion on playing sound effects like this, there is one final thing that we want to talk about: the SoundEffectInstance class. When we play sound effects like this in our game, so far, we have no method of controlling the sound effect, once we have started playing it. So, for example, we can't stop the sound effect while it is going. We can have greater control over the sound effect if we use the SoundEffectInstance class.

To create a SoundEffectInstance, simply call a SoundEffect's CreateInstance method:

SoundEffectInstance soundEffectInstance = effect.CreateInstance();
soundEffectInstance.Play();

Once you have this, you can hang on to it for later and do stuff with the sound effect, even after it has been started. For example, you can use the following line of code to completely stop the sound effect from playing:

soundEffectInstance.Stop();

You may notice that the SoundEffectInstance class has built-in methods for changing the volume, pitch, and pan as well, which you can use to change things while the sound effect plays.

soundEffectInstance.Volume = 0.5f;
soundEffectInstance.Pan = -0.5f;
soundEffectInstance.Pitch = 0.5f;

Pausing and Stopping a Sound Effect

Using a SoundEffectInstance, you can make a sound effect pause or stop completely. This is done with the Pause and Stop methods, which work like this:

soundEffectInstance.Pause();
soundEffectInstance.Stop();

When a sound effect has been paused, if you use the same SoundEffectInstance and play it again, it will continue where it left off, and finish playing the sound effect.

Looping a Sound Effect

Using a SoundEffectInstance, you can make a sound effect loop repeatedly. (Be warned, you'll get sick of it really quickly.)

SoundEffectInstance soundEffectInstance = effect.CreateInstance();
soundEffectInstance.IsLooped = true;
soundEffectInstance.Play();

Checking a Sound Effect's State

The last thing we want to do in this tutorial is to make sure we know how to check what state a sound effect is currently in (playing, paused, or stopped). As an example, this allows us to know when a sound effect is done playing, so we can move on to something else.

This is done by checking against the SoundEffectInstance's State property for SoundState.Playing, SoundState.Paused, or SoundState.Stopped.

For example:

if(soundEffectInstance.State == SoundState.Stopped)
{
    // ... 
}

What's Next?

Now that we know how to play sound effects, we will go on and look at how to play some background music in a similar manner. After that, we will take a look at how to play sound using XACT, which, don't forget, is probably the better way to play sound.


Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!