AudioManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. using Microsoft.Xna.Framework.Media;
  15. #endregion
  16. namespace HoneycombRush
  17. {
  18. /// <summary>
  19. /// Component that manages audio playback for all sounds.
  20. /// </summary>
  21. public class AudioManager : GameComponent
  22. {
  23. #region Fields
  24. /// <summary>
  25. /// The singleton for this type.
  26. /// </summary>
  27. static AudioManager audioManager = null;
  28. public static AudioManager Instance
  29. {
  30. get
  31. {
  32. return audioManager;
  33. }
  34. }
  35. static readonly string soundAssetLocation = "Sounds/";
  36. // Audio Data
  37. Dictionary<string, SoundEffectInstance> soundBank;
  38. Dictionary<string, Song> musicBank;
  39. #endregion
  40. #region Initialization
  41. private AudioManager(Game game)
  42. : base(game) { }
  43. /// <summary>
  44. /// Initialize the static AudioManager functionality.
  45. /// </summary>
  46. /// <param name="game">The game that this component will be attached to.</param>
  47. public static void Initialize(Game game)
  48. {
  49. audioManager = new AudioManager(game);
  50. audioManager.soundBank = new Dictionary<string, SoundEffectInstance>();
  51. audioManager.musicBank = new Dictionary<string, Song>();
  52. game.Components.Add(audioManager);
  53. }
  54. #endregion
  55. #region Loading Methodes
  56. /// <summary>
  57. /// Loads a single sound into the sound manager, giving it a specified alias.
  58. /// </summary>
  59. /// <param name="contentName">The content name of the sound file. Assumes all sounds are located under
  60. /// the "Sounds" folder in the content project.</param>
  61. /// <param name="alias">Alias to give the sound. This will be used to identify the sound uniquely.</param>
  62. /// <remarks>Loading a sound with an alias that is already used will have no effect.</remarks>
  63. public static void LoadSound(string contentName, string alias)
  64. {
  65. SoundEffect soundEffect = audioManager.Game.Content.Load<SoundEffect>(soundAssetLocation + contentName);
  66. SoundEffectInstance soundEffectInstance = soundEffect.CreateInstance();
  67. if (!audioManager.soundBank.ContainsKey(alias))
  68. {
  69. audioManager.soundBank.Add(alias, soundEffectInstance);
  70. }
  71. }
  72. /// <summary>
  73. /// Loads a single song into the sound manager, giving it a specified alias.
  74. /// </summary>
  75. /// <param name="contentName">The content name of the sound file containing the song. Assumes all sounds are
  76. /// located under the "Sounds" folder in the content project.</param>
  77. /// <param name="alias">Alias to give the song. This will be used to identify the song uniquely.</param>
  78. /// /// <remarks>Loading a song with an alias that is already used will have no effect.</remarks>
  79. public static void LoadSong(string contentName, string alias)
  80. {
  81. Song song = audioManager.Game.Content.Load<Song>(soundAssetLocation + contentName);
  82. if (!audioManager.musicBank.ContainsKey(alias))
  83. {
  84. audioManager.musicBank.Add(alias, song);
  85. }
  86. }
  87. /// <summary>
  88. /// Loads and organizes the sounds used by the game.
  89. /// </summary>
  90. public static void LoadSounds()
  91. {
  92. LoadSound("10SecondCountDown", "10SecondCountDown");
  93. LoadSound("30SecondWarning", "30SecondWarning");
  94. LoadSound("BeeBuzzing_Loop", "BeeBuzzing_Loop");
  95. LoadSound("Defeat", "Defeat");
  96. LoadSound("DepositingIntoVat_Loop", "DepositingIntoVat_Loop");
  97. LoadSound("FillingHoneyPot_Loop", "FillingHoneyPot_Loop");
  98. LoadSound("HighScore", "HighScore");
  99. LoadSound("HoneyPotBreak", "HoneyPotBreak");
  100. LoadSound("SmokeGun_Loop", "SmokeGun_Loop");
  101. LoadSound("Stung", "Stung");
  102. LoadSound("Stunned", "Stunned");
  103. LoadSound("Victory", "Victory");
  104. }
  105. /// <summary>
  106. /// Loads and organizes the music used by the game.
  107. /// </summary>
  108. public static void LoadMusic()
  109. {
  110. //LoadSong("InGameSong_Loop","InGameSong_Loop");
  111. //LoadSong("MenuMusic_Loop","MenuMusic_Loop");
  112. }
  113. #endregion
  114. #region Sound Methods
  115. /// <summary>
  116. /// Indexer. Return a sound instance by name.
  117. /// </summary>
  118. public SoundEffectInstance this[string soundName]
  119. {
  120. get
  121. {
  122. if (audioManager.soundBank.ContainsKey(soundName))
  123. {
  124. return audioManager.soundBank[soundName];
  125. }
  126. else
  127. {
  128. return null;
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Plays a sound by name.
  134. /// </summary>
  135. /// <param name="soundName">The name of the sound to play.</param>
  136. public static void PlaySound(string soundName)
  137. {
  138. // If the sound exists, start it
  139. if (audioManager.soundBank.ContainsKey(soundName))
  140. {
  141. audioManager.soundBank[soundName].Play();
  142. }
  143. }
  144. /// <summary>
  145. /// Plays a sound by name.
  146. /// </summary>
  147. /// <param name="soundName">The name of the sound to play.</param>
  148. /// <param name="isLooped">Indicates if the sound should loop.</param>
  149. public static void PlaySound(string soundName, bool isLooped)
  150. {
  151. // If the sound exists, start it
  152. if (audioManager.soundBank.ContainsKey(soundName))
  153. {
  154. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  155. {
  156. audioManager.soundBank[soundName].IsLooped = isLooped;
  157. }
  158. audioManager.soundBank[soundName].Play();
  159. }
  160. }
  161. /// <summary>
  162. /// Plays a sound by name.
  163. /// </summary>
  164. /// <param name="soundName">The name of the sound to play.</param>
  165. /// <param name="isLooped">Indicates if the sound should loop.</param>
  166. /// <param name="volume">Indicates if the volume</param>
  167. public static void PlaySound(string soundName, bool isLooped, float volume)
  168. {
  169. // If the sound exists, start it
  170. if (audioManager.soundBank.ContainsKey(soundName))
  171. {
  172. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  173. {
  174. audioManager.soundBank[soundName].IsLooped = isLooped;
  175. }
  176. audioManager.soundBank[soundName].Volume = volume;
  177. audioManager.soundBank[soundName].Play();
  178. }
  179. }
  180. /// <summary>
  181. /// Stops a sound mid-play. If the sound is not playing, this
  182. /// method does nothing.
  183. /// </summary>
  184. /// <param name="soundName">The name of the sound to stop.</param>
  185. public static void StopSound(string soundName)
  186. {
  187. // If the sound exists, stop it
  188. if (audioManager.soundBank.ContainsKey(soundName))
  189. {
  190. audioManager.soundBank[soundName].Stop();
  191. }
  192. }
  193. /// <summary>
  194. /// Stops all currently playing sounds.
  195. /// </summary>
  196. public static void StopSounds()
  197. {
  198. foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
  199. {
  200. if (sound.State != SoundState.Stopped)
  201. {
  202. sound.Stop();
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Pause or resume all sounds.
  208. /// </summary>
  209. /// <param name="resumeSounds">True to resume all paused sounds or false
  210. /// to pause all playing sounds.</param>
  211. public static void PauseResumeSounds(bool resumeSounds)
  212. {
  213. SoundState state = resumeSounds ? SoundState.Paused : SoundState.Playing;
  214. foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
  215. {
  216. if (sound.State == state)
  217. {
  218. if (resumeSounds)
  219. {
  220. sound.Resume();
  221. }
  222. else
  223. {
  224. sound.Pause();
  225. }
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// Play music by name. This stops the currently playing music first. Music will loop until stopped.
  231. /// </summary>
  232. /// <param name="musicSoundName">The name of the music sound.</param>
  233. /// <remarks>If the desired music is not in the music bank, nothing will happen.</remarks>
  234. public static void PlayMusic(string musicSoundName)
  235. {
  236. // If the music sound exists
  237. if (audioManager.musicBank.ContainsKey(musicSoundName))
  238. {
  239. // Stop the old music sound
  240. if (MediaPlayer.State != MediaState.Stopped)
  241. {
  242. MediaPlayer.Stop();
  243. }
  244. try
  245. {
  246. MediaPlayer.IsRepeating = true;
  247. }
  248. catch (UnauthorizedAccessException)
  249. {
  250. // Simply do nothing. This will happen if the Zune application is launched.
  251. }
  252. try
  253. {
  254. MediaPlayer.Play(audioManager.musicBank[musicSoundName]);
  255. }
  256. catch (InvalidOperationException)
  257. {
  258. // Simply do nothing. This will happen if the Zune application is launched.
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Stops the currently playing music.
  264. /// </summary>
  265. public static void StopMusic()
  266. {
  267. if (MediaPlayer.State != MediaState.Stopped)
  268. {
  269. MediaPlayer.Stop();
  270. }
  271. }
  272. #endregion
  273. #region Instance Disposal Methods
  274. /// <summary>
  275. /// Clean up the component when it is disposing.
  276. /// </summary>
  277. protected override void Dispose(bool disposing)
  278. {
  279. try
  280. {
  281. if (disposing)
  282. {
  283. foreach (var item in soundBank)
  284. {
  285. item.Value.Dispose();
  286. }
  287. soundBank.Clear();
  288. soundBank = null;
  289. }
  290. }
  291. finally
  292. {
  293. base.Dispose(disposing);
  294. }
  295. }
  296. #endregion
  297. }
  298. }