Demonstrates how to initialize the audio engine; load a sound bank and wave bank; and play, pause, resume, or stop a sound (called a cue).
This example builds off a simpler example, How To: Play a Sound.
Note |
---|
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. The complete sample includes the standard solution files and any additional support files required by the sample.
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 StopOrPauseSound { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; // Audio objects AudioEngine engine; SoundBank soundBank; WaveBank waveBank; Cue cue; GamePadState oldState; public Game1() { graphics = new GraphicsDeviceManager(this); } protected override void Initialize() { base.Initialize(); // Initialize audio objects. engine = new AudioEngine("Content\\Audio\\StopOrPauseSound.xgs"); soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb"); waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb"); // Get the cue and play it. cue = soundBank.GetCue("music"); cue.Play(); } 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 == oldState.DPad) { return; } // DPad right is 'play/pause'. if (currentState.DPad.Right == ButtonState.Pressed) { if (cue.IsPaused) { cue.Resume(); } else if (cue.IsPlaying) { cue.Pause(); } else { // If stopped, create a new cue. cue = soundBank.GetCue(cue.Name); cue.Play(); } } // DPad left is 'stop'. if (currentState.DPad.Left == ButtonState.Pressed) { cue.Stop(AudioStopOptions.AsAuthored); } oldState = currentState; } 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); } } }
Playing, Pausing, or Stopping a Sound
To play, pause, or stop a sound
-
Create an AudioEngine, WaveBank, and SoundBank at game start.
-
During game update, call the Update method of the AudioEngine to enable the audio engine to process audio data.
-
Retrieve a cue you want to play by calling SoundBank.GetCue, and then store the returned cue value.
-
To play a cue, pause a cue, or stop a cue, call these methods: To play the cue, call Cue.Play; to pause the cue, call Cue.Pause; and to stop the cue entirely, call Cue.Stop.
- 本文固定链接: http://www.wy182000.com/2009/02/22/how-to-stop-or-pause-a-sound/
- 转载请注明: wy182000 于 Studio 发表