XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Tutorial 4: Working With Audio

back to the main tutorial guide page .

Reference: This is the forth tutorial on how to work with the XNACS1Lib library. It is assumed you have read and understand the previous three tutorials:

Goals : This tutorial concentrates on describing how to work with audio:


1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.
If you double click and run the executable program, you will observe:
This application is identical in all ways to that from the third tutorial except in this case a soft background music and audio cues can be heard when significant events happen (e.g., soccer ball collide with window boundary). In this tutorial, we will examine the details of working with audio cues. We will first examine our source code file structure, and then describe how to create wave files to be included in our projects as audio cues.

2. The Source Code Files/Structure

Take a look at the ExampleProgram folder that you have unzipped and you will see a structure almost identical to that of previous tutorial. The only notable difference is a new Audio subfolder in the Content/Resources folder:

Folders/Files Purposes

with wav files.

The *.wav files are the source of the audio cues.

3. The Solution Explorer:

Now double click on the *.sln file in the ExampleProgram folder, to start the project in the IDE. Notice the structure of the SolutionExplorer follows the source code folder structure we have examined:
As illustrated in the above figure, you will notice the Audio folder and the collection of *.wav files. The process of including the *.wav files into the project works identical to that of *.jpg Texture files, where you would right-mouse-button click on the Resources folder and add the existing Audio folder. Any *.wav files can be included in this folder (and thus into your project). For example the Left.wav and Right.wav files are recorded words of "Left" and "Right" from the Microsoft Audio Recording utility software.

3a. Cues: in the
       PlayACue( String cueName) and
       PlayBackgroundAudio( String cueName, ...) 
functions, the cueName refers to names of these *.wav files. Notice that as in the case of jpg image files where we identify the images by their names without the extension. In this case, we will also refer the audio cues by their file names without the .wav extension.

3b. Limitations: Limited audio bank support : We have a very simple audio support where you can only play up to 8 audio cues at the same time.

3d. Hints on working with Audio Cues: Our simple audio architecture support means we have limited audio resources to work with. We should use this resource carefully and strategically to maximize support for feedback to our users (students).

  1. Use it as a hint to events : Use audio cues to inform users that something has happened (e.g., the soccer ball has touched the window boundary).
  2. Use one cue at a time : it can be confusing to the user when more than one cues are playing at the same time, i.e., be mindful when calling PlayACue() more than once during a single UpdateWorld() function. For example, in your application a hero is walking and encountered a gold nugget during the UpdateWorld() function. It may be your desired to play a cue to represent the hero walking, and a separate cue to represent the encountering of the gold nugget. In this case, you should weigh the importance and probably only play one of the cues.
  3. Use shorter cues rather than longer cues: The audio support will honor the requested duration of a cue even if the cue span multiple UpdateWorld() functions. This means, if you start a five minute audio effect, within this five minutes interval other audio effects will be mixed and can cause confusion. In general multiple short audio snippets corresponding to significant events is much more informative than one significant audio segment representing one of the significant events. For example, if five collisions occurred over a period of one seconds. In this case it is much more informative for the user to hear five distinct audio cues (each can be 0.1 seconds in length), than for the user to hear a single one-second audio segment that accurately represents one of the five collisions. With our simple library support, each of your audio cues should probably be less than one second.

4. Examine the Game1.cs source file:

We are finally ready to examine our source code playing of the cues. In this case, when comparing to the source code from the third tutorial, no new instance variables are necessary and no extra code is necessary in the InitializeWorld() function. We simple call PlayBackgroundAodio() in the beginning and call PlayACue() funciton in the UpdateWorld() function when " interesting " events occur:

protected override void UpdateWorld() {

  A. Update the Rotating ladder

     #region B. Update the soccer ball, make sure it bounces off the window bounds
          BoundCollideStatus collideStatus = CollideWorldBound(m_SoccerBall);
          switch (collideStatus) {
              case BoundCollideStatus .CollideTop:
               case BoundCollideStatus .CollideBottom:
                    PlayACue( "CollideWorld" );
                    break ;
               case BoundCollideStatus .CollideLeft:
                    PlayACue( "Left" );
                    break ;
               case BoundCollideStatus .CollideRight:
                    PlayACue( "Right" );
                    break ;


     please refer to the source file for the rest of the PlayACue() calls.

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.