How To: Stop or Pause a Sound Using XACT

Demonstrates how to initialize the audio engine; load a sound bank and wave bank; and play, pause, resume, or stop a sound (called a cue).

This example builds off a simpler example, How To: Play a Sound Using XACT.

This example assumes you already built an XACT sound bank and wave bank. To learn how to do this, see How To: Add a Sound File to Your Game Using XACT.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample. The complete sample includes the standard solution files and any additional support files required by the sample.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace StopOrPauseSound
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;

        // Audio objects
        AudioEngine engine;
        SoundBank soundBank;
        WaveBank waveBank;
        Cue cue;

        GamePadState oldState;

        public Game1()
            graphics = new GraphicsDeviceManager(this);

        protected override void Initialize()

            // Initialize audio objects.
            engine = new AudioEngine("Content\\Audio\\StopOrPauseSound.xgs");
            soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb");
            waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb");

            // Get the cue and play it.
            cue = soundBank.GetCue("music");

        protected override void LoadContent()

        protected override void UnloadContent()

        protected void UpdateInput()
            // Get the current gamepad state.
            GamePadState currentState = GamePad.GetState(PlayerIndex.One);

            if (currentState.DPad == oldState.DPad)

            // DPad right is 'play/pause'.
            if (currentState.DPad.Right == ButtonState.Pressed)
                if (cue.IsPaused)
                else if (cue.IsPlaying)
                    // If stopped, create a new cue.
                    cue = soundBank.GetCue(cue.Name);

            // DPad left is 'stop'.
            if (currentState.DPad.Left == ButtonState.Pressed)

            oldState = currentState;

        protected override void Update(GameTime gameTime)
            // Allow the game to exit.
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 

            // Check input.

            // Update the audio engine.


        protected override void Draw(GameTime gameTime)

Playing, Pausing, or Stopping a Sound

To play, pause, or stop a sound

  1. Create an AudioEngine, WaveBank, and SoundBank at game start.

  2. During game update, call the Update method of the AudioEngine to enable the audio engine to process audio data.

  3. Retrieve a cue you want to play by calling SoundBank.GetCue, and then store the returned cue value.

  4. To play a cue, pause a cue, or stop a cue, call these methods: To play the cue, call Cue.Play; to pause the cue, call Cue.Pause; and to stop the cue entirely, call Cue.Stop.

Community Additions