首页 > Computer > XNA > How To: Stream a Sound
2009
02-22

How To: Stream a Sound

Demonstrates how to create and use a streaming wave bank.

This topic builds on concepts in the Audio Overview topic, and How To: Add a Sound File to Your Game Using XACT.

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

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 StreamSound {     public class Game1 : Microsoft.Xna.Framework.Game     {         GraphicsDeviceManager graphics;         SpriteBatch spriteBatch;          // Audio objects         AudioEngine engine;         SoundBank soundBank;         WaveBank waveBank;          public Game1()         {             graphics = new GraphicsDeviceManager(this);             Content.RootDirectory = "Content";         }          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");             // Create streaming wave bank.             waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb", 0, 4);              // must call update             engine.Update();              // Play the sound.             soundBank.PlayCue("kaboom");         }          protected override void LoadContent()         {             // Create a new SpriteBatch, which can be used to draw textures.             spriteBatch = new SpriteBatch(GraphicsDevice);              // TODO: use this.Content to load your game content here         }          protected override void UnloadContent()         {         }          protected override void Update(GameTime gameTime)         {             // Allows 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);         }     } } 

Latency

In this context, latency is the amount of time that a device takes to retrieve waves from memory and subsequently play the sound. If latency is substantial, it can cause delays in audio playback, or possibly it can interrupt game sounds altogether.

Why Stream?

A wave bank may be designated as in-memory, which means all of the waves are loaded from the storage medium into game memory. Otherwise, a wave bank may be designated as streaming, which means that waves remain stored on disk. When a wave bank is designated as streaming, the sound information is delivered to the game through in-memory buffers. This is useful for sounds that are too large or too long to completely load into working memory. Streaming allows a game developer to effectively use large amounts of audio data without using too much memory.

When to Stream

Most often it is better to store wave banks in-memory. When waves are in-memory, the playback of the sound always occurs without latency. However, in cases when the sounds are too large to load into memory, a streaming wave bank may be a better choice. For example, this could be the case with background music that is much longer than other game sounds, such as short sound effects.

Creating a Streaming Wave Bank

Using XACT

The process for creating the streaming wave bank in XACT is the same as the process described in How To: Add a Sound File to Your Game Using XACT, with the exception of one step. When creating the new wave bank, select thestreaming option in the dialog box.

In Code

You can create a streaming wave bank by using the WaveBank (AudioEngine, String, Int32, Int16) constructor that requires two streaming-related parameters: offset and packet size. An offset of zero will start the stream at the beginning of the wave file. Packet size determines how much memory a streaming wave bank uses for buffering. A smaller packet size will result in smaller amounts of data streaming through the buffer at any given time. A larger packet size indicates a larger amount of data moving through the buffer. Ideally, a game developer will specify a packet size that uses the least amount of memory, while at the same time allows for uninterrupted sounds during game play.

Dd231917.note(en-US,XNAGameStudio.30).gifNote
Before you execute code to play any sounds, you must call the Update method of the AudioEngine that was used to create the streaming wave bank. Calling the Update method prepares the streaming wave bank for use. If you try to use the wave bank before you call the Update method, an InvaidOperation exception will be thrown.

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

留下一个回复