Creating a Simple 3D Game with XNA/Sound
Readying Your Sound
editUsing sound effects in XNA can be a little long winded, as an external application, the XACT tool (available in the tools folder of your XNA start menu folder) is required to process the sound files into a 'cue' file before it can be used in your project. It is possible to play a '.Wav' file directly in the code, but you lose the benefit of being able to adjust the attributes of your sounds as you will see below. Open up XACT v3, and you should see the following screen.
For this project, we will be using two sound effects, an open licence sound effect file which will be played every time a fish is collided with, and a looping music file taken from the Newgrounds Audio Portal, a good resource for indie games (the game 'Castle Crashers' available on the Xbox Arcade takes all its music from this website) and can be used freely. Both are available in the complete sample on the main page. Note that the sounds will have to be converted into a '.Wav' file before they can be used, for this I used the audio conversion utility dBpoweramp.
Each of the sounds which we will be loading will be stored inside a 'Wave Bank', containing the raw audio data which will be streamed by the program at run time. The instructions on how to play these files will then be stored inside a 'Sound Bank', which contains the 'Sounds' which contain the properties of each sound to be played, and the 'Cue', which will be referenced in the program.
Create a new project (in my case called 'FishSounds'), and right click on the 'Wave Banks' tag on the left side of the screen, and select 'New Wave Bank'. Right click on the new Wave Bank and select 'Insert Wave Files', and open the two wave files.
Next, right click on your Sound Bank, and create a new one. Right click on the new bank and create two new 'Sounds', one named 'Effect', and another called 'Music'. Drag the entries from the 'Wave Bank' to the respective 'Sound Banks'. If you select one of the new 'Sound Banks', you can see a set of options on the left side. The only one we really have to worry about here is the 'Looping'/'Infinite' checkbox near the top, but feel free to adjust any of the others to taste. Click on your 'Music' sound and click the checkbox as below.
Create two new cues the same way as the sounds, and same as before rename them to 'Effect' and 'Music', and drag the respective 'Sound's into them.
Now save your project, and go back into your game project. Right click on your 'Content' folder and add the newly created 'FishSounds.xap' project file.
Create the following 3 new class variables;
AudioEngine audioEngine;
WaveBank waveBank;
SoundBank soundBank;
And the following lines to your 'LoadContent()' method.
audioEngine = new AudioEngine("Content\\FishSounds.xgs");//The new audio project
waveBank = new WaveBank(audioEngine, "Content\\Wave Bank.xwb");//The name of the Wave bank
soundBank = new SoundBank(audioEngine, "Content\\Sound Bank.xsb");//The name of the Sound bank
If you compile the code at this point and it bring up an error message about sound effect not being found, change the path of the '.Wav' file in the XACT.
Now, in order to play the background music, insert the following line directly after your above initialisation code.
soundBank.PlayCue("Music");
and change your collision detection code to the following.
if (CheckCollision(FishMen.Translation, OtherFishes[i].Translation)
&&
OtherFishes[i].clip == OtherFishes[i].skinningData.AnimationClips["Frown"])
{
OtherFishes[i].PlayAnimation("Smile");
soundBank.PlayCue("Effect");
}
Run your code now, and you should have a fully functioning sound system.