AudioManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.IO;
  12. using System.Collections.Generic;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.Media;
  16. #endregion
  17. namespace NetRumble
  18. {
  19. /// <summary>
  20. /// Component that manages audio playback for all sound effects.
  21. /// </summary>
  22. public class AudioManager : GameComponent
  23. {
  24. #region Singleton
  25. /// <summary>
  26. /// The singleton for this type
  27. /// </summary>
  28. private static AudioManager audioManager = null;
  29. #endregion
  30. #region Audio Data
  31. /// <summary>
  32. /// File list of all wav audio files
  33. /// </summary>
  34. private FileInfo[] audioFileList;
  35. /// <summary>
  36. /// Content folder containing audio files
  37. /// </summary>
  38. private DirectoryInfo audioFolder;
  39. /// <summary>
  40. /// Collection of all loaded sound effects
  41. /// </summary>
  42. private static Dictionary<string, SoundEffect> soundList;
  43. /// <summary>
  44. /// Looping song used as the in-game soundtrack
  45. /// </summary>
  46. private static Song soundtrack;
  47. #endregion
  48. #region Initialization Methods
  49. /// <summary>
  50. /// Constructs the manager for audio playback of all sound effects.
  51. /// </summary>
  52. /// <param name="game">The game that this component will be attached to.</param>
  53. /// <param name="audioFolder">The directory containing audio files.</param>
  54. private AudioManager(Game game, DirectoryInfo audioDirectory )
  55. : base(game)
  56. {
  57. try
  58. {
  59. audioFolder = audioDirectory;
  60. audioFileList = audioFolder.GetFiles("*.xnb");
  61. soundList = new Dictionary<string, SoundEffect>();
  62. for (int i = 0; i < audioFileList.Length; i++)
  63. {
  64. string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name);
  65. soundList[soundName] = game.Content.Load<SoundEffect>("audio\\wav\\"+ soundName);
  66. soundList[soundName].Name = soundName;
  67. }
  68. //soundtrack = game.Content.Load<Song>("One Step Beyond");
  69. }
  70. #if !LINUX
  71. catch (NoAudioHardwareException)
  72. #else
  73. catch (Exception)
  74. #endif
  75. {
  76. // silently fall back to silence
  77. }
  78. }
  79. public static void Initialize(Game game, DirectoryInfo audioDirectory)
  80. {
  81. if (game == null)
  82. return;
  83. audioManager = new AudioManager(game, audioDirectory);
  84. game.Components.Add(audioManager);
  85. }
  86. public static void PlaySoundTrack()
  87. {
  88. if (soundtrack == null)
  89. return;
  90. MediaPlayer.Play(soundtrack);
  91. }
  92. #endregion
  93. #region Sound Play Methods
  94. /// <summary>
  95. /// Plays a fire-and-forget sound effect by name.
  96. /// </summary>
  97. /// <param name="soundName">The name of the sound to play.</param>
  98. public static void PlaySoundEffect(string soundName)
  99. {
  100. if (audioManager == null || soundList == null)
  101. return;
  102. if (soundList.ContainsKey(soundName))
  103. {
  104. soundList[soundName].Play();
  105. }
  106. }
  107. /// <summary>
  108. /// Plays a sound effect by name and returns an instance of that sound.
  109. /// </summary>
  110. /// <param name="soundName">The name of the sound to play.</param>
  111. /// <param name="looped">True if sound effect should loop.</param>
  112. /// <param name="instance">The SoundEffectInstance created for this sound effect.</param>
  113. public static void PlaySoundEffect(string soundName, bool looped, out SoundEffectInstance instance)
  114. {
  115. instance = null;
  116. if (audioManager == null || soundList == null)
  117. return;
  118. if (soundList.ContainsKey(soundName))
  119. {
  120. try
  121. {
  122. instance = soundList[soundName].CreateInstance();
  123. if (instance != null)
  124. {
  125. instance.IsLooped = looped;
  126. instance.Play();
  127. }
  128. }
  129. catch (InstancePlayLimitException)
  130. {
  131. // silently fail (returns null instance) if instance limit reached
  132. }
  133. }
  134. }
  135. #endregion
  136. }
  137. }