Sure I can give it a shot,
To get audio into your XNA game project you need to get your audio built and prepared for use with the XNA Audio Engine first.
To do this you will need to open up the XACT Cross-Platform Audio Creation tool that is provided in the XNA Game Studio 2.0 tools folder.
Using this tool is somewhat of a pain and there is an alternative to using this tool but we will get into that later. Once the tool is open and ready to use the next thing you will be required to do is create a new Wave Bank.
Do not be fooled by the project that you will be looking at when you first start up the XACT Cross-Platform Audio Creation tool as it is not a working project and thusly can not add anything to that project so you will create a new project by selecting File->New Project from the program menu or by pressing Ctrl-N. This will open the New Project Path dialog box where you can navigate to where on the hard drive you want to store your XAP project file, give it a name and press the Save button on the New Project Path dialog box to complete the creation.
Now that we have a new XAP project to work with we need to create a couple more project containers such as the Wave Bank and Sound Bank containers that will be used to hold any audio and sound related items needed by the XNA game project. Adding the Wave Bank and Sound Bank containers is accomplished by selecting Wave Bank->New Wave Bank and Sound Bank->New Sound Bank XACT Cross-Platform Audio Tool application menu options to create each of the containers respectively,
The next step to getting audio into your XNA game project is to start adding audio files to the Wave Bank container. To do this you can use the Wave Bank->Insert Wave File(s)... menu option or simply drag your WAVE file from windows explorer into the Wave Bank Container. Once you have all the wave files you intend to use in your game it is time to create the associated Sound and Cue, Sound Bank container objects.
At this stage open both the Wave Bank and Sound Bank container windows and arrange them so they are both visible with no overlapping. We do this so that we have an unobstructed area to drag Wave entries from the Wave Bank container into Sound Name, Sound Bank container. So with that said select the waves that you wish to use in your game from the Wave Bank container and drag them to the Sound Name container of the the currently open Sound Bank. If this step has been performed correctly you should see Sound Name entries that match their respective Wave Bank entries in the display window.
Once the Sound entries have been created we need to create the respective Cue objects that the XNA Audio Engine will use to play your audio files from in-game. To do this we follow the same procedure as we did when creating new Sound Name entries from the Wave Bank by selecting the Sound Name entries from the Sound Bank container that we wish to play and dragging them into the Cue Name container. If this stage has been performed correctly you should see Cue Name entries that match their respective Sound Name entries in the display window.
We are almost there!
Now that we have created and populated the Wave and Sound Banks you can save your project or experiment with the settings and properties provided to you by the XACT Cross-Platform Audio Creation Tool and tailor your audio requirements before saving. Once the XAP project has been saved we can proceed to compiling the project into the desired binary output to be used by the XNA Audio Engine. To do this simply select File->Build... from the XACT Cross-Platform Audio Creation Tool menu or press F7. Doing this will bring up the Build Project dialog asking for a report filename and reporting style for this build process. What this does is allows for the selection of verbose or concise logging of XactBld operations to a file to be reviewed later should there be build problems. We will just leave this at the default settings of None and click the Finish button to complete the build process resulting in the binary files needed by the XNA Audio Engine.
Once this final XACT stage has been completed successfully, save your XAP project one more time before closing the XACT Cross-Platform Audio Creation Tool as the compile process touches the XAP file modifying some internal values and does not save those changes.
All that work and we are only half way there. Now that the hassle of preparing XACT and the audio for use with XNA we can get to some of the code that you could use to play the audio from the XNA Audio Engine. Initializing the engine and such is a very simple task as illustrated by the following code snippet.
| // Insert this code into the global space of your game application |
| public AudioEngine audioEngine; |
| public WaveBank waveBank; |
| public SoundBank soundBank; |
| |
| // Insert this code into the Initialize() method of your game application |
| audioEngine = new AudioEngine("Path_To_XACT_Binary_Output\\XACT_Project_Name.xgs"); |
| waveBank = new WaveBank(audioEngine,"Path_To_XACT_Binary_Output\\Wave Bank.xwb"); |
| soundBank = new SoundBank(audioEngine,"Path_To_XACT_Binary_Output\\Sound Bank.xsb"); |
| |
| |
Now all that is left really is to play the sound someplace through out your game. This is very easy and can be accomplished with a single line of code as illustrated by the following code snippet.
| // The following line of code will play the cue as defined through the XACT tool. |
| soundBank.PlayCue("Cue_Name_To_Play"); |
| |
| |
That should about cover the basics of getting audio into your game using XACT. There is an alternative that I wrote that allows you to automate this process aptly names QuickXAP that takes most if not all of the legwork outlined about above out of the equation allowing developers to add audio media straight into their XNA Game Studio 2.0 projects or as a stand alone tool to automate the creation process of the XAP project file. You can find a link to QuickXAP in these forums under the Audio category for more information.