Playing 2D Sound
We’re now ready to play the sounds we just created. Start up a new
Windows Game project. We’re going to map each sound to a differ
-
ent key so we can play whatever sounds we want. The first thing we
need to do is add the AudioEngine, WaveBank, and SoundBank
objects and create them along with some Cue objects and a pair of
GamePadState objects to keep track of when controls are being
pressed and released:
Listing 5-1
AudioEngine audio;
WaveBank wavebank;
SoundBank soundbank;
Cue titleCue;
Cue zapCue;
Cue wailCue;
Cue dyingCue;
GamePadState _curState;
GamePadState _lastState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
audio = new AudioEngine("Chapter5sounds.xgs");
wavebank = new WaveBank(audio, "Wave Bank.xwb");
soundbank = new SoundBank(audio, "Sound Bank.xsb");
}
We’ll load up the Cue objects in the LoadGraphicsContent method:
Listing 5-2
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
titleCue = soundbank.GetCue("title");
zapCue = soundbank.GetCue("zap");
194 Chapter 5
wailCue = soundbank.GetCue("wail");
dyingCue = soundbank.GetCue("dying");
}
}
The only other thing we need is some code in our Update method:
Listing 5-3
protected override void Update(GameTime gameTime)
{
_curState = GamePad.GetState(PlayerIndex.One);
// Allows the game to exit
if (_curState.Buttons.Back == ButtonState.Pressed)
this.Exit();
if (_curState.Buttons.A == ButtonState.Pressed)
{
if (!titleCue.IsPlaying)
{
titleCue = soundbank.GetCue("title");
titleCue.Play();
}
}
if (_curState.Buttons.B == ButtonState.Pressed)
{
if (!zapCue.IsPlaying)
{
zapCue = soundbank.GetCue("zap");
zapCue.Play();
}
}
if (_curState.Buttons.X == ButtonState.Pressed)
{
if (!wailCue.IsPlaying)
{
wailCue = soundbank.GetCue("wail");
wailCue.Play();
}
}
if (_curState.Buttons.Y == ButtonState.Pressed)
{
if (!dyingCue.IsPlaying)
Sound 195
{
dyingCue = soundbank.GetCue("dying");
dyingCue.Play();
}
}
if (_curState.DPad.Up == ButtonState.Pressed &&
_lastState.DPad.Up == ButtonState.Released)
{
soundbank.PlayCue("title");
}
if (_curState.DPad.Down == ButtonState.Pressed &&
_lastState.DPad.Down == ButtonState.Released)
{
soundbank.PlayCue("zap");
}
if (_curState.DPad.Left == ButtonState.Pressed &&
_lastState.DPad.Left == ButtonState.Released)
{
soundbank.PlayCue("wail");
}
if (_curState.DPad.Right == ButtonState.Pressed &&
_lastState.DPad.Right == ButtonState.Released)
{
soundbank.PlayCue("dying");
}
_lastState = _curState;
base.Update(gameTime);
}
Here we see the two different ways cues can be played. You can
either get a cue from the soundbank and keep it for playing over and
over or you can have the soundbank play it directly.
Let’s make a quick change to the buttons so that we can pause
and resume a cue. Make the following change to each button:
196 Chapter 5
Listing 5-4
if (_curState.Buttons.A == ButtonState.Pressed &&
_lastState.Buttons.A == ButtonState.Released)
{
if (!titleCue.IsPlaying)
{
titleCue = soundbank.GetCue("title");
titleCue.Play();
}
else if (titleCue.IsPaused)
titleCue.Resume();
else if (titleCue.IsPlaying)
titleCue.Pause();
}
Note that the order of your checks here is important. If you reverse
the checks for IsPaused and IsPlaying, you’ll end up pausing the
sound and never being able to get it started again, since IsPlaying
will return true when the sound is paused. We also need to now
check the last state of the buttons to prevent the sound from imme-
diately being paused. Try removing the check of _lastState and see
what happens when you press the A button.
Let’s make another modification to change the volume of the
sounds. We’ll replace the code in the D-pad buttons to do this:
Listing 5-5
if (_curState.DPad.Up == ButtonState.Pressed)
{
volume = MathHelper.Clamp(volume + 0.01f, 0.0f, 5.0f);
}
if (_curState.DPad.Down == ButtonState.Pressed)
{
volume = MathHelper.Clamp(volume - 0.01f, 0.0f, 5.0f);
}
category.SetVolume(volume);
The category object is an AudioCategory object and the volume
variable is a float. That means we need some new declarations and
some code to get the category object:
Sound 197

Get Essential XNA Game Studio 2.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.