首页 > Computer > XNA > How To: Change Sound Volume Levels
2009
02-22

How To: Change Sound Volume Levels

Demonstrates how to initialize the audio engine and how to use categories to change the playback volume of a group of sounds.

Categories control the sound volume levels for groups of sounds in XACT. These categories are collections that can hold one or more references to cues. Global variables linked to runtime parameter controls (RPC) within the XACT project control the individual cue volumes. This topic explains how to use categories to control the volume of groups of sounds.

For more information about using 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 automatically adjusts the volume based on distance, see How To: Apply Attenuation and Doppler 3D Audio Effects.

Bb195052.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.

C#
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 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()         {             base.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.             soundBank.PlayCue("music");         }          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)             {                 mus
icVolume = 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.             musicCategory.SetVolume(musicVolume);         }          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);         }     } } 

Changing Sound Volume Levels

To change sound volume levels

  1. Create an AudioEngineWaveBank 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. To retrieve a category of sounds whose volume you want to change, call AudioEngine.GetCategory, and then pass in the name of the category you created in the XACT project.

  4. Call AudioCategory.SetVolume on the retrieved category.

  5. Specify the desired volume.

    Typically, your preferred volume will be between 0.0f (silence) and 1.0f (full volume, as authored).

  6. 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.

最后编辑:
作者:wy182000
这个作者貌似有点懒,什么都没有留下。

留下一个回复