All pastes #932976 Raw Edit

Untitled

public text v1 · immutable
·published 2008-03-08 01:22 UTC
rendered paste body
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace Pong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class AudioManager : Microsoft.Xna.Framework.GameComponent
    {
        private AudioEngine engine;
        private SoundBank sounds;
        private WaveBank waves;
        private Cue musicCue;

        public AudioManager(Game game, string settingsFile, string waveBankFile, string SoundBankFile)
            : base(game)
        {
            // TODO: Construct any child components here
            this.engine = new AudioEngine(settingsFile);
            this.waves = new WaveBank(this.engine, waveBankFile);
            this.sounds = new SoundBank(this.engine, SoundBankFile);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public Cue GetCue(string cueName)
        {
            return sounds.GetCue(cueName);
        }

        public void PlayCue(string cueName)
        {
            sounds.PlayCue(cueName);
        }

        public void PlayMusic(string cueName)
        {
            this.StopMusic();
            this.musicCue = this.GetCue(cueName);
            this.musicCue.Play();
        }

        public void StopMusic()
        {
            if (this.musicCue != null)
            {
                this.musicCue.Stop(AudioStopOptions.AsAuthored);
                this.musicCue.Dispose();
                this.musicCue = null;
            }
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    this.StopMusic();
                    if (this.sounds != null)
                    {
                        this.sounds.Dispose();
                        this.sounds = null;
                    }
                    if (this.waves != null)
                    {
                        this.waves.Dispose();
                        this.waves = null;
                    }
                    if (this.engine != null)
                    {
                        this.engine.Dispose();
                        this.engine = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

    }
}