AudioManager.cs 4.5 KB

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