Using XNA Sound in Forms App

Tuesday, November 29 2011 - , , ,

At work we had a need to create a Win Forms Application that needed to use 3D audio and I wanted to use XNA to do it, it was pretty simple to do but there was no good example to do just this on the net. You could also do something like this to create a Console App or Library.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using System.Windows.Threading;

namespace SoundTest
{
    public partial class Form1 : Form
    {
        ServiceContainer service;
        SoundEffect soundEffect;
        ContentManager content;
        SoundEffectInstance soundEffectInstance;
        AudioEmitter emitter = new AudioEmitter();
        AudioListener listener = new AudioListener();
       
        public Form1()
        {
            InitializeComponent();
            service = new ServiceContainer();
            content = new ContentManager(service, "Content");
            soundEffect = content.Load<SoundEffect>("sound");

            soundEffectInstance = soundEffect.CreateInstance();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // This plays a sound in a 3D space but at this point you can use any of the other sound functions in the XNA API
            // Right
            SoundEffectInstance i = soundEffect.CreateInstance();
            emitter.Position = new Vector3(1, 0, 0);
            i.Apply3D(listener, emitter);
            i.Play();
        }
    }

    public class ServiceContainer : IServiceProvider
    {
        Dictionary<Type, object> services = new Dictionary<Type, object>();
        private DispatcherTimer frameworkDispatcherTimer;

        /// <summary>
        /// Adds a new service to the collection.
        /// </summary>
        public void AddService<T>(T service)
        {
            services.Add(typeof(T), service);
        }

        /// <summary>
        /// Looks up the specified service.
        /// </summary>
        public object GetService(Type serviceType)
        {
            object service;

            services.TryGetValue(serviceType, out service);

            return service;
        }
       
        public ServiceContainer()
        {           
            this.frameworkDispatcherTimer = new DispatcherTimer();
            this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333);
            this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
            FrameworkDispatcher.Update();
        }
        void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }               
    }
}
 

4 comment(s) so far

This is really a good example for making win forms application to use XNA 3D sound.

I was really attracted by such a good product , I think only a good product to be able to get customer support, only honest companies can be sustainable development.

We offer women online, enjoy the at . Match the tiffany with the cheap and . We offer top quality of online. Welcome to shop.

We offer women online, enjoy the at . Match the tiffany with the cheap and . We offer top quality of online. Welcome to shop.

Post your comment

Comment