AudioManager.cs 10 KB

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