Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How To: Stop or Pause a Sound

How To: Stop or Pause a Sound

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.30).gifNote
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()
        {
            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);
        }
    }
}

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

ADD
Show:
© 2015 Microsoft