AudioManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AudioManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. #endregion
  15. namespace MemoryMadness
  16. {
  17. /// <summary>
  18. /// Component that manages audio playback for all sounds.
  19. /// </summary>
  20. public class AudioManager : GameComponent
  21. {
  22. #region Fields
  23. #region Singleton
  24. /// <summary>
  25. /// The singleton for this type.
  26. /// </summary>
  27. static AudioManager audioManager = null;
  28. public static AudioManager Instance
  29. {
  30. get { return audioManager; }
  31. }
  32. public static bool IsInitialized
  33. {
  34. get { return audioManager.isInitialized; }
  35. }
  36. #endregion
  37. #region Audio Data
  38. SoundEffectInstance musicSound;
  39. Dictionary<string, SoundEffectInstance> soundBank;
  40. string[,] soundNames;
  41. #endregion
  42. bool isInitialized;
  43. #endregion
  44. #region Initialization
  45. private AudioManager(Game game)
  46. : base(game) { }
  47. /// <summary>
  48. /// Initialize the static AudioManager functionality.
  49. /// </summary>
  50. /// <param name="game">The game that this component will be attached to.</param>
  51. public static void Initialize(Game game)
  52. {
  53. audioManager = new AudioManager(game);
  54. game.Components.Add(audioManager);
  55. }
  56. #endregion
  57. #region Loading Methodes
  58. /// <summary>
  59. /// Loads a sounds and organizes them for future usage
  60. /// </summary>
  61. public static void LoadSounds()
  62. {
  63. string soundLocation = "Sounds/";
  64. audioManager.soundNames = new string[,]
  65. {
  66. {"RedButton", "red"},
  67. {"GreenButton", "green"},
  68. {"BlueButton", "blue"},
  69. {"YellowButton", "yellow"},
  70. {"HighScoreScreen", "doorOpen"},
  71. {"LevelComplete", "success"},
  72. {"DefeatBuzzer", "fail"}
  73. };
  74. audioManager.soundBank = new Dictionary<string, SoundEffectInstance>();
  75. for (int i = 0; i < audioManager.soundNames.GetLength(0); i++)
  76. {
  77. SoundEffect se = audioManager.Game.Content.Load<SoundEffect>(
  78. soundLocation + audioManager.soundNames[i, 0]);
  79. audioManager.soundBank.Add(
  80. audioManager.soundNames[i, 1], se.CreateInstance());
  81. }
  82. audioManager.isInitialized = true;
  83. }
  84. #endregion
  85. #region Sound Methods
  86. /// <summary>
  87. /// Indexer. Return a sound instance by name
  88. /// </summary>
  89. public SoundEffectInstance this[string soundName]
  90. {
  91. get
  92. {
  93. if (audioManager.soundBank.ContainsKey(soundName))
  94. return audioManager.soundBank[soundName];
  95. else
  96. return null;
  97. }
  98. }
  99. /// <summary>
  100. /// Plays a sound by name.
  101. /// </summary>
  102. /// <param name="soundName">The name of the sound to play</param>
  103. public static void PlaySound(string soundName)
  104. {
  105. // If the sound exists, start it
  106. if (audioManager.soundBank.ContainsKey(soundName))
  107. audioManager.soundBank[soundName].Play();
  108. }
  109. /// <summary>
  110. /// Plays a sound by name.
  111. /// </summary>
  112. /// <param name="soundName">The name of the sound to play</param>
  113. /// <param name="isLooped">Indicates if the sound should loop</param>
  114. public static void PlaySound(string soundName, bool isLooped)
  115. {
  116. // If the sound exists, start it
  117. if (audioManager.soundBank.ContainsKey(soundName))
  118. {
  119. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  120. audioManager.soundBank[soundName].IsLooped = isLooped;
  121. audioManager.soundBank[soundName].Play();
  122. }
  123. }
  124. /// <summary>
  125. /// Stops a sound mid-play. If the sound is not playing, this
  126. /// method does nothing.
  127. /// </summary>
  128. /// <param name="soundName">The name of the sound to stop</param>
  129. public static void StopSound(string soundName)
  130. {
  131. // If the sound exists, stop it
  132. if (audioManager.soundBank.ContainsKey(soundName))
  133. audioManager.soundBank[soundName].Stop();
  134. }
  135. /// <summary>
  136. /// Stops all currently playing sounds.
  137. /// </summary>
  138. public static void StopSounds()
  139. {
  140. foreach (var sound in audioManager.soundBank.Values)
  141. {
  142. if (sound.State != SoundState.Stopped)
  143. {
  144. sound.Stop();
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// Checks whether or not sounds are currently playing.
  150. /// </summary>
  151. /// <returns>True if some sounds are playing, false otherwise.</returns>
  152. public static bool AreSoundsPlaying()
  153. {
  154. foreach (var sound in audioManager.soundBank.Values)
  155. {
  156. if (sound.State == SoundState.Playing)
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /// <summary>
  164. /// Pause or resume all sounds.
  165. /// </summary>
  166. /// <param name="resumeSounds">True to resume all paused sounds or false
  167. /// to pause all playing sounds.</param>
  168. public static void PauseResumeSounds(bool resumeSounds)
  169. {
  170. SoundState state = resumeSounds ? SoundState.Paused : SoundState.Playing;
  171. foreach (var sound in audioManager.soundBank.Values)
  172. {
  173. if (sound.State == state)
  174. {
  175. if (resumeSounds)
  176. {
  177. sound.Resume();
  178. }
  179. else
  180. {
  181. sound.Pause();
  182. }
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// Play music by sound name.
  188. /// </summary>
  189. /// <param name="musicSoundName">The name of the music sound.</param>
  190. public static void PlayMusic(string musicSoundName)
  191. {
  192. // Stop the old music sound
  193. if (audioManager.musicSound != null)
  194. audioManager.musicSound.Stop(true);
  195. // If the music sound exists
  196. if (audioManager.soundBank.ContainsKey(musicSoundName))
  197. {
  198. // Get the instance and start it
  199. audioManager.musicSound = audioManager.soundBank[musicSoundName];
  200. if (!audioManager.musicSound.IsLooped)
  201. audioManager.musicSound.IsLooped = true;
  202. audioManager.musicSound.Play();
  203. }
  204. }
  205. #endregion
  206. #region Instance Disposal Methods
  207. /// <summary>
  208. /// Clean up the component when it is disposing.
  209. /// </summary>
  210. protected override void Dispose(bool disposing)
  211. {
  212. try
  213. {
  214. if (disposing)
  215. {
  216. foreach (var item in soundBank)
  217. {
  218. item.Value.Dispose();
  219. }
  220. soundBank.Clear();
  221. soundBank = null;
  222. }
  223. }
  224. finally
  225. {
  226. base.Dispose(disposing);
  227. }
  228. }
  229. #endregion
  230. }
  231. }