AudioManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 Blackjack
  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("Bet", "Bet");
  93. LoadSound("CardFlip", "Flip");
  94. LoadSound("CardsShuffle", "Shuffle");
  95. LoadSound("Deal", "Deal");
  96. }
  97. /// <summary>
  98. /// Loads and organizes the music used by the game.
  99. /// </summary>
  100. public static void LoadMusic()
  101. {
  102. LoadSong("InGameSong_Loop","InGameSong_Loop");
  103. LoadSong("MenuMusic_Loop","MenuMusic_Loop");
  104. }
  105. #endregion
  106. #region Sound Methods
  107. /// <summary>
  108. /// Indexer. Return a sound instance by name.
  109. /// </summary>
  110. public SoundEffectInstance this[string soundName]
  111. {
  112. get
  113. {
  114. if (audioManager.soundBank.ContainsKey(soundName))
  115. {
  116. return audioManager.soundBank[soundName];
  117. }
  118. else
  119. {
  120. return null;
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Plays a sound by name.
  126. /// </summary>
  127. /// <param name="soundName">The name of the sound to play.</param>
  128. public static void PlaySound(string soundName)
  129. {
  130. // If the sound exists, start it
  131. if (audioManager.soundBank.ContainsKey(soundName))
  132. {
  133. audioManager.soundBank[soundName].Play();
  134. }
  135. }
  136. /// <summary>
  137. /// Plays a sound by name.
  138. /// </summary>
  139. /// <param name="soundName">The name of the sound to play.</param>
  140. /// <param name="isLooped">Indicates if the sound should loop.</param>
  141. public static void PlaySound(string soundName, bool isLooped)
  142. {
  143. // If the sound exists, start it
  144. if (audioManager.soundBank.ContainsKey(soundName))
  145. {
  146. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  147. {
  148. audioManager.soundBank[soundName].IsLooped = isLooped;
  149. }
  150. audioManager.soundBank[soundName].Play();
  151. }
  152. }
  153. /// <summary>
  154. /// Plays a sound by name.
  155. /// </summary>
  156. /// <param name="soundName">The name of the sound to play.</param>
  157. /// <param name="isLooped">Indicates if the sound should loop.</param>
  158. /// <param name="volume">Indicates if the volume</param>
  159. public static void PlaySound(string soundName, bool isLooped, float volume)
  160. {
  161. // If the sound exists, start it
  162. if (audioManager.soundBank.ContainsKey(soundName))
  163. {
  164. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  165. {
  166. audioManager.soundBank[soundName].IsLooped = isLooped;
  167. }
  168. audioManager.soundBank[soundName].Volume = volume;
  169. audioManager.soundBank[soundName].Play();
  170. }
  171. }
  172. /// <summary>
  173. /// Stops a sound mid-play. If the sound is not playing, this
  174. /// method does nothing.
  175. /// </summary>
  176. /// <param name="soundName">The name of the sound to stop.</param>
  177. public static void StopSound(string soundName)
  178. {
  179. // If the sound exists, stop it
  180. if (audioManager.soundBank.ContainsKey(soundName))
  181. {
  182. audioManager.soundBank[soundName].Stop();
  183. }
  184. }
  185. /// <summary>
  186. /// Stops all currently playing sounds.
  187. /// </summary>
  188. public static void StopSounds()
  189. {
  190. foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
  191. {
  192. if (sound.State != SoundState.Stopped)
  193. {
  194. sound.Stop();
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Pause or resume all sounds.
  200. /// </summary>
  201. /// <param name="resumeSounds">True to resume all paused sounds or false
  202. /// to pause all playing sounds.</param>
  203. public static void PauseResumeSounds(bool resumeSounds)
  204. {
  205. SoundState state = resumeSounds ? SoundState.Paused : SoundState.Playing;
  206. foreach (SoundEffectInstance sound in audioManager.soundBank.Values)
  207. {
  208. if (sound.State == state)
  209. {
  210. if (resumeSounds)
  211. {
  212. sound.Resume();
  213. }
  214. else
  215. {
  216. sound.Pause();
  217. }
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// Play music by name. This stops the currently playing music first. Music will loop until stopped.
  223. /// </summary>
  224. /// <param name="musicSoundName">The name of the music sound.</param>
  225. /// <remarks>If the desired music is not in the music bank, nothing will happen.</remarks>
  226. public static void PlayMusic(string musicSoundName)
  227. {
  228. // If the music sound exists
  229. if (audioManager.musicBank.ContainsKey(musicSoundName))
  230. {
  231. // Stop the old music sound
  232. if (MediaPlayer.State != MediaState.Stopped)
  233. {
  234. MediaPlayer.Stop();
  235. }
  236. MediaPlayer.IsRepeating = true;
  237. MediaPlayer.Play(audioManager.musicBank[musicSoundName]);
  238. }
  239. }
  240. /// <summary>
  241. /// Stops the currently playing music.
  242. /// </summary>
  243. public static void StopMusic()
  244. {
  245. if (MediaPlayer.State != MediaState.Stopped)
  246. {
  247. MediaPlayer.Stop();
  248. }
  249. }
  250. #endregion
  251. #region Instance Disposal Methods
  252. /// <summary>
  253. /// Clean up the component when it is disposing.
  254. /// </summary>
  255. protected override void Dispose(bool disposing)
  256. {
  257. try
  258. {
  259. if (disposing)
  260. {
  261. foreach (var item in soundBank)
  262. {
  263. item.Value.Dispose();
  264. }
  265. soundBank.Clear();
  266. soundBank = null;
  267. }
  268. }
  269. finally
  270. {
  271. base.Dispose(disposing);
  272. }
  273. }
  274. #endregion
  275. }
  276. }