首页 > Computer > XNA > How To: Play a Sound Using XACT
2009
02-22

How To: Play a Sound Using XACT

Demonstrates how to initialize the audio engine, load sound and wave banks, and play a sound by using a Cue object.

Dd231915.note(en-US,XNAGameStudio.30).gifNote
This example assumes you already saved an XACT project (.xap file). 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 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 PlaySound {     public class Game1 : Microsoft.Xna.Framework.Game     {         GraphicsDeviceManager graphics;          // Audio objects         AudioEngine engine;         SoundBank soundBank;         WaveBank waveBank;          public Game1()         {             graphics = new GraphicsDeviceManager(this);         }          protected override void Initialize()         {             base.Initialize();              // Initialize audio objects.             engine = new AudioEngine("Content\\Audio\\PlaySound.xgs");             soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb");             waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb");              // Play the sound.             soundBank.PlayCue("kaboom");         }          protected override void LoadContent()         {         }          protected override void UnloadContent()         {         }          protected override void Update(GameTime gameTime)         {             // Allow the game to exit.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                 this.Exit();              // Update the audio engine.             engine.Update();              base.Update(gameTime);         }          protected override void Draw(GameTime gameTime)         {             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);             base.Draw(gameTime);         }     } } 

Each Cue instance that you play is unique, even when you play multiple cues with the same name. This enables you to play multiple instances of the same Cue simultaneously.

Playing a Sound

To insert the desired .xap file into your project

  1. With your XNA Game Studio game loaded in Visual Studio, right-click your content icon in the Solut
    ion Explorer pane.

  2. Click Add, click New Folder, and then name the new folder “Audio.”

  3. Move your XACT project file (.xap) and .wav file or files into the new “Audio” file created in the previous step.

    The new folder will exist in the Content folder, under your game project folder.

  4. Again, with your XNA Game Studio game loaded in Visual Studio, right-click the Audio file the Solution Explorer pane.

  5. Click Add, and then click Existing Item.

  6. Select the .xap file from the Audio folder.

    The .xap file is then inserted into your project. By default, it is processed by the content pipeline, and built wave and sound banks are accessed automatically when the game is built.

  7. Edit your game code.

    When following these steps and using the above code, be sure your file names match those given in the example.

To play the sound

  1. When your game starts, create a new AudioEngine.

  2. Pass in the name of the XACT global settings file output by the XACT project file.

    Make sure the name includes an .xgs extension.

  3. For each wave bank in your XACT project, create a new WaveBank, and then pass in the audio engine and the name of the XACT wave bank file output by the XACT project file.

    Make sure the name includes an .xwb extension.

  4. For each sound bank in your XACT project, create a new SoundBank, and then pass in the audio engine and the name of the XACT sound bank file output by the XACT project file.

    Make sure the name includes an .xsb extension.

  5. During game update, call the Update method of the AudioEngine to allow the audio engine to process audio data.

  6. Call the PlayCue method from your SoundBank, and then pass in the name of the cue you wish to play.

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

留下一个回复