#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion
namespace Xillian
{
///
/// Component that manages audio playback for all cues.
///
public class AudioManager : GameComponent
{
#region Fields
///
/// The audio engine used to play all cues.
///
private AudioEngine engine;
///
/// The soundbank that contains all cues.
///
private SoundBank sounds;
///
/// The wavebank with all wave files for this game.
///
private WaveBank waves;
///
/// The music cue for the game
///
private Cue musicCue;
#endregion
#region Initialization
///
/// Constructs the manager for audio playback of all cues.
///
/// The game that this component will be attached to.
/// The filename of the XACT settings file.
/// The filename of the XACT wavebank file.
/// The filename of the XACT soundbank file.
public AudioManager(Game game, string settingsFile, string waveBankFile,
string soundBankFile)
: base(game)
{
engine = new AudioEngine(settingsFile);
waves = new WaveBank(engine, waveBankFile);
sounds = new SoundBank(engine, soundBankFile);
}
#endregion
#region Cues
///
/// Retrieve a cue by name.
///
/// The name of the cue requested.
/// The cue corresponding to the name provided.
public Cue GetCue(string cueName)
{
return sounds.GetCue(cueName);
}
///
/// Plays a cue by name.
///
/// The name of the cue to play.
public void PlayCue(string cueName)
{
sounds.PlayCue(cueName);
}
#endregion
#region Music
///
/// Plays the music for this game, by name.
///
/// The name of the music cue to play.
public void PlayMusic(string cueName)
{
// if music is already playing, stop it
StopMusic();
// start the new music cue
musicCue = GetCue(cueName);
musicCue.Play();
}
///
/// Stop music playback.
///
public void StopMusic()
{
if (musicCue != null)
{
musicCue.Stop(AudioStopOptions.AsAuthored);
musicCue.Dispose();
musicCue = null;
}
}
#endregion
#region Update
///
/// Update the audio system.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
engine.Update();
}
#endregion
#region Disposing
///
/// Clean up the component when it is disposing.
///
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
StopMusic();
if (sounds != null)
{
sounds.Dispose();
sounds = null;
}
if (waves != null)
{
waves.Dispose();
waves = null;
}
if (engine != null)
{
engine.Dispose();
engine = null;
}
}
}
finally
{
base.Dispose(disposing);
}
}
#endregion
}
}