Advertisement
DaveVoyles

AudioManager Class

May 29th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AudioManager.cs
  4. //
  5. // This is the original, un-edited sample from Microsoft.
  6. --------------------------------------------------------------------------------
  7. #endregion
  8.  
  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.  
  18. namespace NetRumble
  19. {
  20.     /// <summary>
  21.     /// Component that manages audio playback for all sound effects.
  22.     /// </summary>
  23.     public class AudioManager : GameComponent
  24.     {
  25.         #region Singleton
  26.  
  27.  
  28.         /// <summary>
  29.         /// The singleton for this type
  30.         /// </summary>
  31.         private static AudioManager audioManager = null;
  32.  
  33.  
  34.         #endregion
  35.  
  36.  
  37.         #region Audio Data
  38.  
  39.         /// <summary>
  40.         /// File list of all wav audio files
  41.         /// </summary>
  42.         private FileInfo[] audioFileList;
  43.  
  44.         /// <summary>
  45.         /// Content folder containing audio files
  46.         /// </summary>
  47.         private DirectoryInfo audioFolder;
  48.        
  49.         /// <summary>
  50.         /// Collection of all loaded sound effects
  51.         /// </summary>
  52.         private static Dictionary<string, SoundEffect> soundList;
  53.  
  54.         /// <summary>
  55.         /// Looping song used as the in-game soundtrack
  56.         /// </summary>
  57.         private static Song soundtrack;
  58.  
  59.         #endregion
  60.  
  61.  
  62.         #region Initialization Methods
  63.  
  64.         /// <summary>
  65.         /// Constructs the manager for audio playback of all sound effects.
  66.         /// </summary>
  67.         /// <param name="game">The game that this component will be attached to.</param>
  68.         /// <param name="audioFolder">The directory containing audio files.</param>
  69.         private AudioManager(Game game, DirectoryInfo audioDirectory )
  70.             : base(game)
  71.         {
  72.             try
  73.             {
  74.                 audioFolder = audioDirectory;
  75.                 audioFileList = audioFolder.GetFiles("*.xnb");
  76.                 soundList = new Dictionary<string, SoundEffect>();
  77.  
  78.                 for (int i = 0; i < audioFileList.Length; i++)
  79.                 {
  80.                     string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name);
  81.                     soundList[soundName] = game.Content.Load<SoundEffect>("audio\\wav\\"+ soundName);
  82.                     soundList[soundName].Name = soundName;
  83.                 }
  84.  
  85.                 soundtrack = game.Content.Load<Song>("One Step Beyond");
  86.             }
  87.             catch (NoAudioHardwareException)
  88.             {
  89.                 // silently fall back to silence
  90.             }
  91.         }
  92.  
  93.         public static void Initialize(Game game, DirectoryInfo audioDirectory)
  94.         {
  95.             if (game == null)
  96.                 return;
  97.  
  98.             audioManager = new AudioManager(game, audioDirectory);
  99.             game.Components.Add(audioManager);
  100.         }
  101.  
  102.         public static void PlaySoundTrack()
  103.         {
  104.             if (soundtrack == null)
  105.                 return;
  106.  
  107.             MediaPlayer.Play(soundtrack);
  108.         }
  109.  
  110.         #endregion
  111.  
  112.  
  113.         #region Sound Play Methods
  114.  
  115.         /// <summary>
  116.         /// Plays a fire-and-forget sound effect by name.
  117.         /// </summary>
  118.         /// <param name="soundName">The name of the sound to play.</param>
  119.         public static void PlaySoundEffect(string soundName)
  120.         {
  121.             if (audioManager == null || soundList == null)
  122.                 return;
  123.  
  124.             if (soundList.ContainsKey(soundName))
  125.             {
  126.                 soundList[soundName].Play();
  127.             }
  128.         }
  129.  
  130.         /// <summary>
  131.         /// Plays a sound effect by name and returns an instance of that sound.
  132.         /// </summary>
  133.         /// <param name="soundName">The name of the sound to play.</param>
  134.         /// <param name="looped">True if sound effect should loop.</param>
  135.         /// <param name="instance">The SoundEffectInstance created for this sound effect.</param>
  136.         public static void PlaySoundEffect(string soundName, bool looped, out SoundEffectInstance instance)
  137.         {
  138.             instance = null;
  139.             if (audioManager == null || soundList == null)
  140.                 return;
  141.  
  142.             if (soundList.ContainsKey(soundName))
  143.             {
  144.                 try
  145.                 {
  146.                     instance = soundList[soundName].CreateInstance();
  147.                     if (instance != null)
  148.                     {
  149.                         instance.IsLooped = looped;
  150.                         instance.Play();
  151.                     }
  152.                 }
  153.                 catch (InstancePlayLimitException)
  154.                 {
  155.                     // silently fail (returns null instance) if instance limit reached
  156.                 }
  157.             }
  158.         }
  159.  
  160.         #endregion
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement