Playing Sounds from an XACT Project

This section demonstrates how to initialize the audio engine, to load sound banks and wave banks created using XACT, and then to play a sound by using a Cue object.

Complete Sample

The code in this topic shows you the techniques for initializing, loading, and playing sounds. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download PlaySound_Sample.zip

Playing a Sound Using XACT

After you have an XACT project that has sound files, you then can load and play those sounds in your game. To add sound files to an XACT project, see Adding a Sound File (XACT).

To play a sound using sound files created by XACT

When following these steps and using the code provided, ensure your file names match those given in the example.

  1. Allocate audio objects AudioEngine (XACT Engine), SoundBank, and WaveBank.

    // Audio objects
    AudioEngine engine;
    SoundBank soundBank;
    WaveBank waveBank;
    
  2. Initialize the audio objects by loading the XACT sound files, and then use a Cue object to play the sound.

    // Initialize audio objects.
    engine = new AudioEngine("Content\\PlaySound.xgs");
    soundBank = new SoundBank(engine, "Content\\Sound Bank.xsb");
    waveBank = new WaveBank(engine, "Content\\Wave Bank.xwb");
    
    // Play the sound.
    Cue cue = soundBank.GetCue("kaboom");
    cue.Play();
    

Each Cue instance that you play is unique, even when you play multiple cues with the same name. This enables you to play multiple instances of the same Cue simultaneously.

Concepts

  • Adding a Sound File (XACT)
    Demonstrates how to add wave (.wav) files to an XACT project that can be built and interpreted by an XNA Game Studio game to play audio.
  • Pausing a Sound (XACT)
    Demonstrates how to initialize the audio engine; how to load a sound bank and wave bank; and how to play, pause, resume, and stop a sound (called a cue).
  • Playing a Sound
    Demonstrates how to play a simple sound by using SoundEffect.

Reference

  • AudioEngine Class
    Represents the audio engine. Applications use the methods of the audio engine to instantiate and manipulate core audio objects.
  • Cue Class
    Defines methods for managing the playback of sounds.
  • SoundBank Class
    Represents a sound bank, which is a collection of cues.
  • WaveBank Class
    Represents a wave bank, which is a collection of wave files.