Export (0) Print
Expand All

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.
Bb203880.note(en-US,XNAGameStudio.20).gifNote
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.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

The code in this article illustrates the technique described in the text. For a complete code sample, download the StopOrPauseASound Sample from the XNA Creators Club Online Web site. The complete sample includes the standard solution files and any additional support files required by the sample.

using System;
using System.Collections.Generic;
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.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()
        {
            base.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");
            cue.Play();
        }

        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)
            {
                return;
            }

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

            // DPad left is 'stop'.
            if (currentState.DPad.Left == ButtonState.Pressed)
            {
                cue.Stop(AudioStopOptions.AsAuthored);
            }

            oldState = currentState;
        }

        protected override void Update(GameTime gameTime)
        {
            // Allow the game to exit.
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // Check input.
            UpdateInput();

            // Update the audio engine.
            engine.Update();

            base.Update(gameTime);
        }

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

Community Additions

ADD
Show:
© 2014 Microsoft