Share via


How to: Play a Sound

This example demonstrates how to initialize the audio engine, load sound and wave banks, and play a sound via a Cue object.

Note

This example assumes you have already saved an XACT project (.xap file). To learn how to do this, see How to: Add a Sound File to Your Game Using XACT.

To play a sound

  1. Move the XACT project file (.xap) that you saved into your XNA game project folder (see How to: Add a Sound File to Your Game Using XACT).
  2. Add any wave files the .xap file references to your XNA game project folder.
  3. With your XNA Game loaded in Visual C# Express, right-click your project icon in the Solution Explorer pane.
  4. Click Add, and then click Existing Item.
  5. Select the .xap file from your XNA game project folder. The .xap file will be inserted into your project. By default, it will be processed by the Content Pipeline and build wave and sound banks automatically when the game is built.
  6. Edit your game code. When your game starts, create a new AudioEngine, passing in the name of the XACT global settings file output by the XACT project file. Make sure the name includes an .xgs extension.
  7. For each wave bank in your XACT project, create a new WaveBank, passing in the audio engine and the name of the XACT wave bank file output by the XACT project file. Make sure the name includes a .xwb extension.
  8. For each sound bank in your XACT project, create a new SoundBank, passing in the audio engine and the name of the XACT sound bank file output by the XACT project file. Make sure the name includes an .xsb extension.
  9. During game update, call the Update method of the AudioEngine to allow the audio engine to process audio data.
  10. Call the PlayCue method from your SoundBank, passing in the name of the cue you wish to play.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;

    // Audio API components.
    AudioEngine audioEngine;
    WaveBank waveBank;
    SoundBank soundBank;

    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );
    }

    protected override void Initialize()
    {
        audioEngine = new AudioEngine( "TestSounds.xgs" );
        waveBank = new WaveBank( audioEngine, "TestWaveBank.xwb" );
        soundBank = new SoundBank( audioEngine, "TestSoundBank.xsb" );

        soundBank.PlayCue( "kaboom" );

        base.Initialize();
    }

    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {

        }

    }

    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        audioEngine.Update();

        base.Update( gameTime );
    }

    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        base.Draw( gameTime );
    }
}

Remarks

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

See Also

Tasks

How to: Add a Sound File to Your Game Using XACT