Xact Game Component Version 0.5


Here we have a game component for XNA GSE 2.0 intended to make management of Xact audio easier.  You can download an example solution and the code at the end of this post.

The component is easy to use once you have set it up, which unfortunately means you have to use Xact – see here and here for help with that beast.

Also here’s a plug for www.soundsnap.com go there, get free sounds and upload some of your own too.

So I’ll assume you’ve played with the example and now you want to include it into your own project. 

First open your project and add a reference to MI.CommonClasses.Audio.dll

Next add a using statement for the namespace

Then add your Xact project file, but make sure you add it as a link, then any changes you make will be automatically picked up.

Now you will need to create an XML file to hold the settings, you can use audio.xml from the example solution as a guideline.

Everything is setup now so you can start coding.  there are three basic objects/classes you will be interacting with:

IXactAudioManagerService – the interface to the service/component that dose all the work
CueWrapper – encapsulates the cue functionality allowing you to pause/play/stop a cue whist hiding some of the weirder behaviour
CueGroup – gives singe point of control to a collection of CueWrappers

So, in your main game class(or wherever you want to access the component) create a field of type IXactAudioManager:

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        IXactAudioManager XactAudioManagerService;

Then in your main game class constructor add the component to the components collection:

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
           
            //add the component to the componets collection
            this.Components.Add(new XactAudioManager(this, "AudioSettings.xml"));
        }

Now in the Initialize() method of your main game class (or wherever you want to access the component) get a reference to the service interface:

       /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            //get a reference to the service
            XactAudioManagerService = this.Services.GetService(typeof(IXactAudioManager));

        }

Now you can either get a reference to a reusable

CueWrapper cueWrapper = XactAudioManagerService.GetWrappedCue("music", "Explosion", "Default Group");

Which you can play, Pause, Unpause or stop at anytime:

           cueWrapper.Play();
           cueWrapper.Pause();
           cueWrapper.Unpause();
           cueWrapper.Stop();
           cueWrapper.StopImmediate();

Or you can play a one off cue that will be played and the destroyed:

XactAudioManagerService.PlayOneOffCue("music", "Explosion", "Default Group");

Nice and easy.

This component should currently be considered ‘very beta’ – there is no error checking yet, anyway please give it a try and leave any *CONSTRUCTIVE* criticisms.

 http://cid-dfa4d7b321096c3e.skydrive.live.com/embedrow.aspx/XNAFiles/MI.Examples.zip