Export (0) Print
Expand All

How To: Change Sound Volume Levels

This example demonstrates how to initialize the audio engine and change the playback volume of a group of sounds using categories.

Sound volume levels for groups of sounds in XACT are controlled by categories, which are collections that can hold one or more references to cues. Individual cue volumes are controlled within by global variables linked to runtime parameter controls (RPCs) within the XACT project. This article explains how to use categories to control the volume of groups of sounds.

For more information on how to use global variables and RPCs, see XACT Variables, and XACT Runtime Parameter Controls. For more information on creating categories within your XACT project, see Making XACT Categories. To see how XACT can automatically adjust volume based on distance, see How To: Apply Attenuation and Doppler 3D Audio Effects.

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 change sound volume levels

  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 category of sounds whose volume you want to change by calling AudioEngine.GetCategory and passing in the name of the category you created in the XACT project.
  4. Call AudioCategory.SetVolume on the retrieved category. Specify the desired volume, which is typically between 0.0f (silence) and 1.0f (full volume as authored). To make the volume louder than designed, pass in values greater than 1.0f (for example, a volume of 2.0f adds +6 dB to the authored level).

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.

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 ChangeSoundVolume
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;

        // Audio objects
        AudioEngine engine;
        SoundBank soundBank;
        WaveBank waveBank;
        AudioCategory musicCategory;

        // Music volume.
        float musicVolume = 1.0f;

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

        protected override void Initialize()

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

            // Get the category.
            musicCategory = engine.GetCategory("Music");

            // Play the sound.

        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.Up == ButtonState.Pressed)
                musicVolume = MathHelper.Clamp(musicVolume + 0.01f, 0.0f, 2.0f);
            if (currentState.DPad.Down == ButtonState.Pressed)
                musicVolume = MathHelper.Clamp(musicVolume - 0.01f, 0.0f, 2.0f);

            // Set the category volume.

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

            // Check input.

            // Update the audio engine.


        protected override void Draw(GameTime gameTime)

Community Additions

© 2015 Microsoft