Using XACT Projects in you XNA Game


In the previous tutorial, we looked at how to make an XACT project. In this tutorial, we will take a look at how to load the XACT project in our game, and how to use it to play music and sound effects.

Adding the XACT Project to the Game Project

The first step we need to do is to add the sound project to our game. We will do this in the same way that we have done in lots of other tutorials. Go to the Solution Explorer, right-click on the Content folder (or wherever your project was saved at), and choose Add > Add Existing Item from the popup menu that appears. Locate your XACT project (with the .xap extension) and add it to your project. (If you want to review the detailed instructions for how to do this, see the tutorial on adding assets to a game.)

Coding the Sound Engine

Having a well-planned and powerful sound engine is important in a game, but for what we're doing, we only require a very basic sound engine. XNA has a built-inSoundEngine class, which should get us started.

To store the information we need, we need to create three variables. Add the following three variable declarations to your game as instance variables:

private AudioEngine audioEngine;
private WaveBank waveBank;
private SoundBank soundBank;

The first variable is the main sound engine.

Since our project included one wave bank and one sound bank, we will need to include a variable to store the information in each of these as well. the variables waveBank and soundBank will keep track of these.

Now let's actually load the sound information into these variables. I created a separate method that looks like the one below, to perform this task.

private void LoadAudioContent()
    audioEngine = new AudioEngine("Content/SoundDemoAudio.xgs");
    waveBank = new WaveBank(audioEngine, "Content/Wave Bank.xwb");
    soundBank = new SoundBank(audioEngine, "Content/Sound Bank.xsb");

Let's now talk about what these lines of code mean. For starters, you can see that we are simply assigning a value to each of the variables. The waveBank and soundBank variables both also need a reference to their parent audio engine.

All three of them are also given a string as a parameter. When XNA's Content Pipeline builds your XACT project, it will build a global settings file for the entire project (the .xgs file) and a wave bank file (the .xwb file) for every wave bank you have (only 1 in our case), and a sound bank file (the .xsb file) for every sound bank you have in your project. Each of our components needs to know what file to look in for their information, so we pass "SoundDemoAudio.xgs", "Wave Bank.xwb", and "Sound Bank.xsb" to these. Make sure you give the right path for your files! That is one of the most common problems that arise with playing sound. For example, if you put your .xap project in an "Audio" directory, within the "Content" directory, then you should put "Content/Audio/SoundDemoAudio.xgs".

The global settings file will be named the same as your project name. The wave bank files and sound bank files will all be named whatever the wave bank or sound bank was called.

Now, we also need to have our program call this method. So don't forget to add the following line of code to your project where it can be called at startup. I added it to my LoadContent() method, in my main game class.


Playing a Sound Cue

Finally, now, we can play a sound. With the work we have done, this will be pretty easy to do. We can add the following line of code wherever we want to play the sound cue.


The name of the cue in this line must be the name of the cue in the XACT project, which by default is the name of the .wav file that it came from. Yours might be different, depending on what your file and sound cue were called.

In my game, I just want to demonstrate the sound playing, so I've added this line to the LoadAudioContent() method that we just created. Mine plays the explosion sound when the game first starts. You will probably want to configure it so that it plays the sound when a particular event occurs.

There is just one more thing that we need to do. The audio engine needs to be updated frequently so that it can deal with changes that we've told it to make. To accomplish this, in your Update() method, add the following line of code:


You should be able to run your game now and hear the sound playing!

What's Next?

Now you know the basics of playing sound in your game. If you want to know more about audio in your XNA game, I would recommend looking at the tutorial on XACT sound loops. Also, you might find the tutorials on 3D sound interesting. It shows you how to make sounds seem like they are coming from a particular location. Also, I mentioned that XACT only allows you to play .wav files. If you are interested in playing .wma files or .mp3 files in XNA, you should also look at my tutorial for playing .wma and .mp3 files in XNA.

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