共用方式為


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.

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 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. From the category you receive, call AudioCategory.SetVolume, passing in a value from 0.0 (silent) to 1.0 (full volume).
#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;

    // Stored music volume, for dynamic volume change.
    float musicVolume = 1.0f;

    // Audio API components.
    AudioEngine audioEngine;
    WaveBank waveBank;
    SoundBank soundBank;
    AudioCategory music;
    Cue musicCue;

    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" );
        music = audioEngine.GetCategory( "Music" );
        musicCue = soundBank.GetCue( "music" );

        // Play music.
        musicCue.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();

        // Update the AudioEngine.
        audioEngine.Update();

        // Allow the user to dynamically change music volume.
        GamePadState gs = GamePad.GetState( PlayerIndex.One );
        if (gs.DPad.Up == ButtonState.Pressed)
        {
            musicVolume = MathHelper.Clamp( musicVolume + 0.05f, 0.0f, 1.0f );
        }
        if (gs.DPad.Down == ButtonState.Pressed)
        {
            musicVolume = MathHelper.Clamp( musicVolume - 0.05f, 0.0f, 1.0f );
        }

        // Set the volume to whatever the user has changed it to.
        music.SetVolume( musicVolume );

        base.Update( gameTime );
    }

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

        base.Draw( gameTime );
    }
}