2
0

AudioManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. catch (NoAudioHardwareException)
  71. {
  72. // silently fall back to silence
  73. }
  74. }
  75. public static void Initialize(Game game, DirectoryInfo audioDirectory)
  76. {
  77. if (game == null)
  78. return;
  79. audioManager = new AudioManager(game, audioDirectory);
  80. game.Components.Add(audioManager);
  81. }
  82. public static void PlaySoundTrack()
  83. {
  84. if (soundtrack == null)
  85. return;
  86. MediaPlayer.Play(soundtrack);
  87. }
  88. #endregion
  89. #region Sound Play Methods
  90. /// <summary>
  91. /// Plays a fire-and-forget sound effect by name.
  92. /// </summary>
  93. /// <param name="soundName">The name of the sound to play.</param>
  94. public static void PlaySoundEffect(string soundName)
  95. {
  96. if (audioManager == null || soundList == null)
  97. return;
  98. if (soundList.ContainsKey(soundName))
  99. {
  100. soundList[soundName].Play();
  101. }
  102. }
  103. /// <summary>
  104. /// Plays a sound effect by name and returns an instance of that sound.
  105. /// </summary>
  106. /// <param name="soundName">The name of the sound to play.</param>
  107. /// <param name="looped">True if sound effect should loop.</param>
  108. /// <param name="instance">The SoundEffectInstance created for this sound effect.</param>
  109. public static void PlaySoundEffect(string soundName, bool looped, out SoundEffectInstance instance)
  110. {
  111. instance = null;
  112. if (audioManager == null || soundList == null)
  113. return;
  114. if (soundList.ContainsKey(soundName))
  115. {
  116. try
  117. {
  118. instance = soundList[soundName].CreateInstance();
  119. if (instance != null)
  120. {
  121. instance.IsLooped = looped;
  122. instance.Play();
  123. }
  124. }
  125. catch (InstancePlayLimitException)
  126. {
  127. // silently fail (returns null instance) if instance limit reached
  128. }
  129. }
  130. }
  131. #endregion
  132. }
  133. }