AudioManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Audio;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. #endregion
  16. namespace CatapultGame
  17. {
  18. /// <summary>
  19. /// Component that manages audio playback for all sounds.
  20. /// </summary>
  21. public class AudioManager : GameComponent
  22. {
  23. #region Singleton
  24. /// <summary>
  25. /// The singleton for this type.
  26. /// </summary>
  27. private static AudioManager audioManager = null;
  28. #endregion
  29. #region Audio Data
  30. private SoundEffectInstance musicSound;
  31. private Dictionary<string, SoundEffectInstance> soundBank;
  32. private string[,] soundNames;
  33. #endregion
  34. #region Initialization Methods
  35. private AudioManager(Game game)
  36. : base(game) { }
  37. /// <summary>
  38. /// Initialize the static AudioManager functionality.
  39. /// </summary>
  40. /// <param name="game">The game that this component will be attached to.</param>
  41. public static void Initialize(Game game)
  42. {
  43. audioManager = new AudioManager(game);
  44. if (game != null)
  45. {
  46. game.Components.Add(audioManager);
  47. }
  48. }
  49. #endregion
  50. #region Loading Methodes
  51. /// <summary>
  52. /// Loads a sounds and organizes them for future usage
  53. /// </summary>
  54. public static void LoadSounds()
  55. {
  56. string soundLocation = "Sounds/";
  57. audioManager.soundNames = new string[,] {
  58. {"CatapultExplosion", "catapultExplosion"},
  59. {"Lose", "gameOver_Lose"},
  60. {"Win", "gameOver_Win"},
  61. {"BoulderHit", "boulderHit"},
  62. {"CatapultFire", "catapultFire"},
  63. {"RopeStretch", "ropeStretch"}};
  64. audioManager.soundBank = new Dictionary<string, SoundEffectInstance>();
  65. for (int i = 0; i < audioManager.soundNames.GetLength(0); i++)
  66. {
  67. SoundEffect se = audioManager.Game.Content.Load<SoundEffect>(
  68. soundLocation + audioManager.soundNames[i, 0]);
  69. audioManager.soundBank.Add(
  70. audioManager.soundNames[i, 1], se.CreateInstance());
  71. }
  72. }
  73. #endregion
  74. #region Sound Methods
  75. /// <summary>
  76. /// Plays a sound by name.
  77. /// </summary>
  78. /// <param name="soundName">The name of the sound to play</param>
  79. public static void PlaySound(string soundName)
  80. {
  81. // If the sound exists, start it
  82. if (audioManager.soundBank.ContainsKey(soundName))
  83. audioManager.soundBank[soundName].Play();
  84. }
  85. /// <summary>
  86. /// Plays a sound by name.
  87. /// </summary>
  88. /// <param name="soundName">The name of the sound to play</param>
  89. /// <param name="isLooped">Indicates if the sound should loop</param>
  90. public static void PlaySound(string soundName, bool isLooped)
  91. {
  92. // If the sound exists, start it
  93. if (audioManager.soundBank.ContainsKey(soundName))
  94. {
  95. if (audioManager.soundBank[soundName].IsLooped != isLooped)
  96. audioManager.soundBank[soundName].IsLooped = isLooped;
  97. audioManager.soundBank[soundName].Play();
  98. }
  99. }
  100. /// <summary>
  101. /// Stops a sound mid-play. If the sound is not playing, this
  102. /// method does nothing.
  103. /// </summary>
  104. /// <param name="soundName">The name of the sound to stop</param>
  105. public static void StopSound(string soundName)
  106. {
  107. // If the sound exists, stop it
  108. if (audioManager.soundBank.ContainsKey(soundName))
  109. audioManager.soundBank[soundName].Stop();
  110. }
  111. /// <summary>
  112. /// Stops a sound mid-play. If the sound is not playing, this
  113. /// method does nothing.
  114. /// </summary>
  115. /// <param name="soundName">The name of the sound to stop</param>
  116. public static void StopSounds()
  117. {
  118. var soundEffectInstances = from sound in audioManager.soundBank.Values
  119. where sound.State != SoundState.Stopped
  120. select sound;
  121. foreach (var soundeffectInstance in soundEffectInstances)
  122. soundeffectInstance.Stop();
  123. }
  124. /// <summary>
  125. /// Pause or Resume all sounds to support pause screen
  126. /// </summary>
  127. /// <param name="isPause">Should pause or resume?</param>
  128. public static void PauseResumeSounds(bool isPause)
  129. {
  130. SoundState state = isPause ? SoundState.Paused : SoundState.Playing;
  131. var soundEffectInstances = from sound in audioManager.soundBank.Values
  132. where sound.State == state
  133. select sound;
  134. foreach (var soundeffectInstance in soundEffectInstances)
  135. {
  136. if (isPause)
  137. soundeffectInstance.Play();
  138. else
  139. soundeffectInstance.Pause();
  140. }
  141. }
  142. /// <summary>
  143. /// Play music by sound name.
  144. /// </summary>
  145. /// <param name="musicSoundName">The name of the music sound</param>
  146. public static void PlayMusic(string musicSoundName)
  147. {
  148. // Stop the old music sound
  149. if (audioManager.musicSound != null)
  150. audioManager.musicSound.Stop(true);
  151. // If the music sound exists
  152. if (audioManager.soundBank.ContainsKey(musicSoundName))
  153. {
  154. // Get the instance and start it
  155. audioManager.musicSound = audioManager.soundBank[musicSoundName];
  156. if (!audioManager.musicSound.IsLooped)
  157. audioManager.musicSound.IsLooped = true;
  158. audioManager.musicSound.Play();
  159. }
  160. }
  161. #endregion
  162. #region Instance Disposal Methods
  163. /// <summary>
  164. /// Clean up the component when it is disposing.
  165. /// </summary>
  166. protected override void Dispose(bool disposing)
  167. {
  168. try
  169. {
  170. if (disposing)
  171. {
  172. foreach (var item in soundBank)
  173. {
  174. item.Value.Dispose();
  175. }
  176. soundBank.Clear();
  177. soundBank = null;
  178. }
  179. }
  180. finally
  181. {
  182. base.Dispose(disposing);
  183. }
  184. }
  185. #endregion
  186. }
  187. }