AudioManager.cs 6.6 KB

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