#region File Description //----------------------------------------------------------------------------- // AudioManager.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.IO; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Media; #endregion namespace NetRumble { /// /// Component that manages audio playback for all sound effects. /// public class AudioManager : GameComponent { #region Singleton /// /// The singleton for this type /// private static AudioManager audioManager = null; #endregion #region Audio Data /// /// File list of all wav audio files /// private FileInfo[] audioFileList; /// /// Content folder containing audio files /// private DirectoryInfo audioFolder; /// /// Collection of all loaded sound effects /// private static Dictionary soundList; /// /// Looping song used as the in-game soundtrack /// private static Song soundtrack; #endregion #region Initialization Methods /// /// Constructs the manager for audio playback of all sound effects. /// /// The game that this component will be attached to. /// The directory containing audio files. private AudioManager(Game game, DirectoryInfo audioDirectory ) : base(game) { try { audioFolder = audioDirectory; audioFileList = audioFolder.GetFiles("*.xnb"); soundList = new Dictionary(); for (int i = 0; i < audioFileList.Length; i++) { string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name); soundList[soundName] = game.Content.Load("Audio\\wav\\"+ soundName); soundList[soundName].Name = soundName; } //soundtrack = game.Content.Load("One Step Beyond"); } catch (NoAudioHardwareException) { // silently fall back to silence } } public static void Initialize(Game game, DirectoryInfo audioDirectory) { if (game == null) return; audioManager = new AudioManager(game, audioDirectory); game.Components.Add(audioManager); } public static void PlaySoundTrack() { if (soundtrack == null) return; MediaPlayer.Play(soundtrack); } #endregion #region Sound Play Methods /// /// Plays a fire-and-forget sound effect by name. /// /// The name of the sound to play. public static void PlaySoundEffect(string soundName) { if (audioManager == null || soundList == null) return; if (soundList.ContainsKey(soundName)) { soundList[soundName].Play(); } } /// /// Plays a sound effect by name and returns an instance of that sound. /// /// The name of the sound to play. /// True if sound effect should loop. /// The SoundEffectInstance created for this sound effect. public static void PlaySoundEffect(string soundName, bool looped, out SoundEffectInstance instance) { instance = null; if (audioManager == null || soundList == null) return; if (soundList.ContainsKey(soundName)) { try { instance = soundList[soundName].CreateInstance(); if (instance != null) { instance.IsLooped = looped; instance.Play(); } } catch (InstancePlayLimitException) { // silently fail (returns null instance) if instance limit reached } } } #endregion } }