Export (0) Print
Expand All

Pausing a Sound (XACT)

Demonstrates how to initialize the audio engine; how to load a sound bank and wave bank; and how to play, pause, resume, and stop a sound (called a cue). This example builds off a simpler example, Playing Sounds from an XACT Project, and assumes you already built a sound bank and wave bank using the Microsoft Cross-Platform Audio Creation Tool (XACT). If not, see Adding a Sound File (XACT) to learn how to do this.

Complete Sample

The code in this topic shows you the techniques stopping and pausing sounds. 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 .

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.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace StopOrPauseSoundXACT
{
    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\\StopOrPauseSound.xgs");
            soundBank = new SoundBank(engine, "Content\\Sound Bank.xsb");
            waveBank = new WaveBank(engine, "Content\\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. Call the Update method of the AudioEngine during game update to enable the audio engine to process audio data.

  3. Call SoundBank.GetCue to retrieve a cue you want to play, and then store the returned cue value.

To play a cue

To pause a cue

To stop a cue

Community Additions

ADD
Show:
© 2014 Microsoft