#region File Description
//-----------------------------------------------------------------------------
// AudioManager.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
#endregion
namespace HoneycombRush
{
///
/// Component that manages audio playback for all sounds.
///
public class AudioManager : GameComponent
{
#region Fields
///
/// The singleton for this type.
///
static AudioManager audioManager = null;
public static AudioManager Instance
{
get
{
return audioManager;
}
}
static readonly string soundAssetLocation = "Sounds/";
// Audio Data
Dictionary soundBank;
Dictionary musicBank;
#endregion
#region Initialization
private AudioManager(Game game)
: base(game) { }
///
/// Initialize the static AudioManager functionality.
///
/// The game that this component will be attached to.
public static void Initialize(Game game)
{
audioManager = new AudioManager(game);
audioManager.soundBank = new Dictionary();
audioManager.musicBank = new Dictionary();
game.Components.Add(audioManager);
}
#endregion
#region Loading Methodes
///
/// Loads a single sound into the sound manager, giving it a specified alias.
///
/// The content name of the sound file. Assumes all sounds are located under
/// the "Sounds" folder in the content project.
/// Alias to give the sound. This will be used to identify the sound uniquely.
/// Loading a sound with an alias that is already used will have no effect.
public static void LoadSound(string contentName, string alias)
{
SoundEffect soundEffect = audioManager.Game.Content.Load(soundAssetLocation + contentName);
SoundEffectInstance soundEffectInstance = soundEffect.CreateInstance();
if (!audioManager.soundBank.ContainsKey(alias))
{
audioManager.soundBank.Add(alias, soundEffectInstance);
}
}
///
/// Loads a single song into the sound manager, giving it a specified alias.
///
/// The content name of the sound file containing the song. Assumes all sounds are
/// located under the "Sounds" folder in the content project.
/// Alias to give the song. This will be used to identify the song uniquely.
/// /// Loading a song with an alias that is already used will have no effect.
public static void LoadSong(string contentName, string alias)
{
Song song = audioManager.Game.Content.Load(soundAssetLocation + contentName);
if (!audioManager.musicBank.ContainsKey(alias))
{
audioManager.musicBank.Add(alias, song);
}
}
///
/// Loads and organizes the sounds used by the game.
///
public static void LoadSounds()
{
LoadSound("10SecondCountDown", "10SecondCountDown");
LoadSound("30SecondWarning", "30SecondWarning");
LoadSound("BeeBuzzing_Loop", "BeeBuzzing_Loop");
LoadSound("Defeat", "Defeat");
LoadSound("DepositingIntoVat_Loop", "DepositingIntoVat_Loop");
LoadSound("FillingHoneyPot_Loop", "FillingHoneyPot_Loop");
LoadSound("HighScore", "HighScore");
LoadSound("HoneyPotBreak", "HoneyPotBreak");
LoadSound("SmokeGun_Loop", "SmokeGun_Loop");
LoadSound("Stung", "Stung");
LoadSound("Stunned", "Stunned");
LoadSound("Victory", "Victory");
}
///
/// Loads and organizes the music used by the game.
///
public static void LoadMusic()
{
//LoadSong("InGameSong_Loop","InGameSong_Loop");
//LoadSong("MenuMusic_Loop","MenuMusic_Loop");
}
#endregion
#region Sound Methods
///
/// Indexer. Return a sound instance by name.
///
public SoundEffectInstance this[string soundName]
{
get
{
if (audioManager.soundBank.ContainsKey(soundName))
{
return audioManager.soundBank[soundName];
}
else
{
return null;
}
}
}
///
/// Plays a sound by name.
///
/// The name of the sound to play.
public static void PlaySound(string soundName)
{
// If the sound exists, start it
if (audioManager.soundBank.ContainsKey(soundName))
{
audioManager.soundBank[soundName].Play();
}
}
///
/// Plays a sound by name.
///
/// The name of the sound to play.
/// Indicates if the sound should loop.
public static void PlaySound(string soundName, bool isLooped)
{
// If the sound exists, start it
if (audioManager.soundBank.ContainsKey(soundName))
{
if (audioManager.soundBank[soundName].IsLooped != isLooped)
{
audioManager.soundBank[soundName].IsLooped = isLooped;
}
audioManager.soundBank[soundName].Play();
}
}
///
/// Plays a sound by name.
///
/// The name of the sound to play.
/// Indicates if the sound should loop.
/// Indicates if the volume
public static void PlaySound(string soundName, bool isLooped, float volume)
{
// If the sound exists, start it
if (audioManager.soundBank.ContainsKey(soundName))
{
if (audioManager.soundBank[soundName].IsLooped != isLooped)
{
audioManager.soundBank[soundName].IsLooped = isLooped;
}
audioManager.soundBank[soundName].Volume = volume;
audioManager.soundBank[soundName].Play();
}
}
///
/// Stops a sound mid-play. If the sound is not playing, this
/// method does nothing.
///
/// The name of the sound to stop.
public static void StopSound(string soundName)
{
// If the sound exists, stop it
if (audioManager.soundBank.ContainsKey(soundName))
{
audioManager.soundBank[soundName].Stop();
}
}
///
/// Stops all currently playing sounds.
///
public static void StopSounds()
{
foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
{
if (sound.State != SoundState.Stopped)
{
sound.Stop();
}
}
}
///
/// Pause or resume all sounds.
///
/// True to resume all paused sounds or false
/// to pause all playing sounds.
public static void PauseResumeSounds(bool resumeSounds)
{
SoundState state = resumeSounds ? SoundState.Paused : SoundState.Playing;
foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
{
if (sound.State == state)
{
if (resumeSounds)
{
sound.Resume();
}
else
{
sound.Pause();
}
}
}
}
///
/// Play music by name. This stops the currently playing music first. Music will loop until stopped.
///
/// The name of the music sound.
/// If the desired music is not in the music bank, nothing will happen.
public static void PlayMusic(string musicSoundName)
{
// If the music sound exists
if (audioManager.musicBank.ContainsKey(musicSoundName))
{
// Stop the old music sound
if (MediaPlayer.State != MediaState.Stopped)
{
MediaPlayer.Stop();
}
try
{
MediaPlayer.IsRepeating = true;
}
catch (UnauthorizedAccessException)
{
// Simply do nothing. This will happen if the Zune application is launched.
}
try
{
MediaPlayer.Play(audioManager.musicBank[musicSoundName]);
}
catch (InvalidOperationException)
{
// Simply do nothing. This will happen if the Zune application is launched.
}
}
}
///
/// Stops the currently playing music.
///
public static void StopMusic()
{
if (MediaPlayer.State != MediaState.Stopped)
{
MediaPlayer.Stop();
}
}
#endregion
#region Instance Disposal Methods
///
/// Clean up the component when it is disposing.
///
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
foreach (var item in soundBank)
{
item.Value.Dispose();
}
soundBank.Clear();
soundBank = null;
}
}
finally
{
base.Dispose(disposing);
}
}
#endregion
}
}