Share via


How to: Stop or Pause a Sound

This example 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.

Note

This example assumes you have 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.

To stop or pause a sound

  1. Create an AudioEngine, WaveBank, and SoundBank at game start.
  2. During game update, call the Update method of the AudioEngine to allow the audio engine to process audio data.
  3. Retrieve a cue you wish to play by calling SoundBank.GetCue. Store the cue value that is returned.
  4. Once you have played the cue by calling Cue.Play, you may choose to pause the cue by calling Cue.Pause, or stop the cue entirely by calling Cue.Stop.
  5. If you wish to resume a paused cue, you can simply call Cue.Resume.
  6. If you wish to play a stopped cue, you must reacquire the cue by calling SoundBank.GetCue.
#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;

    // Stored cue to pause/stop.
    Cue cue = null;

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

    protected override void Initialize()
    {
        // Initialize audio engine, wave and sound banks.
        audioEngine = new AudioEngine( "TestSounds.xgs" );
        waveBank = new WaveBank( audioEngine, "TestWaveBank.xwb" );
        soundBank = new SoundBank( audioEngine, "TestSoundBank.xsb" );
        cue = soundBank.GetCue( "music" );

        cue.Play();

        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();

        // Allow user to play, pause, resume, or stop cue.
        GamePadState gs = GamePad.GetState( PlayerIndex.One );

        // Can play and pause, or stop.
        if (gs.DPad.Right == ButtonState.Pressed)
        {
            if (cue.IsPaused)
            {
                cue.Resume();
            }
            else if (cue.IsPlaying)
            {
                cue.Pause();
            }
            else
            {
                // Must refetch cue if stopped.
                cue = soundBank.GetCue( cue.Name );
                cue.Play();
            }
        }

        if (gs.DPad.Left == ButtonState.Pressed)
        {
            if (cue.IsPaused || cue.IsPlaying)
            {
                cue.Stop( AudioStopOptions.AsAuthored );
            }
        }

        base.Update( gameTime );
    }

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

        base.Draw( gameTime );
    }
}