InterestGroupDirectx/CnetTutorials/DirectSound9/PlaySoundEffect

Written by ThomasWard This tutorial shows you the basic startup tasks to play a wav file using DirectX version 9 and C#.net. It assumes the following:

If you do not have the DirectX 9 SDK installed on your computer, prior to trying this source code, please download [WWW] The DirectX 9 SDK and install it.

Ok, lets get started. Open up Visual Studio 2003 and start a new C#.net windows application project and name it PlaySound. Once the project appears, you'll see that it has Form1.cs in the solution explorer. Press enter on Form1.cs to open it up. Now, go to the view menu, and select toolbox. Add a button to the form and it will have a default value of button1. Just leave it that for now.

Ok, now you have to set a reference to microsoft.DirectX and Microsoft.DirectX.DirectSound prior to programming anything. You do this by going to the project menu, alt+p, then go to add reference... and press enter or press the letter r. When the new screen appears, in the list of components, down arrow until you get to Microsoft.DirectX. Once on it, press alt+e to activate the select button. It will be added to the list of "selected references". Continue to down arrow until you arive at Microsoft.DirectX.DirectSound. Once on it, press alt+e to activate the select button. Now you have added both references to the "selected components" list. Finally, just tab to the ok button and press enter. Now you have set a reference to Microsoft.DirectX and Microsoft.DirectX.DirectSound. You can now begin to program playing sound effects.

Your project has form1 already added to it. Open up the code editor for Form1.cs and add these using statements to the top of form1 above the line namespace PlaySound:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

Now in the form1 class, just under the text "#Region Windows Form Designer generated code" add these objects...

// sound card device
private Microsoft.DirectX.DirectSound.Device audio = null;
    // sound buffer description
Microsoft.DirectX.DirectSound.BufferDescription soundDesc = null;
// holds sound.wav file data
private Microsoft.DirectX.DirectSound.SecondaryBuffer sound = null;

Now, we need to create a method to initialize our audio device, and set cooperative level flags on the device. The following example demonstrates how to setup the audio device.

// Initialize the audio device.
                public void InitializeAudio()
                {

                        // Create new instance of our
// audio device.
                        audio = new 

Microsoft.DirectX.DirectSound.Device();

                        // Set the cooperative level flags
                        // for our audio device.
                        // flags should be set to normal
                        // for most games.
                        audio.SetCooperativeLevel(this, 

CooperativeLevel.Normal);
                }

At this point, we can create a method to set our sound properties, and load our sound effects in to memory. The following method loads the sound effects, and sets initial buffer properties.

public void LoadSoundEffects()
                {
                        // Create a buffer description object.
                        soundDesc =
 new Microsoft.DirectX.DirectSound.BufferDescription();
                        // Do we want to set frequency?
                        soundDesc.ControlFrequency = true;
// Do we want effects to have global focus?
soundDesc.GlobalFocus = true;
                // Do we want to pan the sound?
                        soundDesc.ControlPan = true;
// Do we want to set volume?
soundDesc.ControlVolume = true;
// Load our sound effect.
                        sound = new 

Microsoft.DirectX.DirectSound.SecondaryBuffer
                                (@"Sound.wav", soundDesc, audio);
}

Now, all we must really do to finish setting up our audio is to have the Main method call the two methods we just created above.

static void Main() 
                {
                        using(Form1 form = new Form1())
                        {
                                // Initialize the audio device.
                                form.InitializeAudio();
                                // Load our sounds.
                                form.LoadSoundEffects();
                                // Start the application.
                                Application.Run(form);
                        }
                }

At this point, we have our sound card setup, the wav file loaded into memory and ready to be used. We'll play the sound effect in the button1 method. Here is the code:

private void button1_Click(object sender, System.EventArgs e)
                {
                sound.Play(0, BufferPlayFlags.Default);
                }

Here are some more things you'll want to know when it comes to playing sounds:

// to stop a sound you use
                        sound.Stop();
// To set the sound position
                        // you use
                        sound.SetCurrentPosition(0); 
// to pause a sound you use
                        sound.Stop();
// To pan a sound left
                        // you use any negative number from
                        // -1 to -9000
                        sound.Pan = -45000;
// To pan a sound right
                        // you use any number from
                        // 1 to 9000
                        sound.Pan = 45000;
                                // To change the frequency of 
                        // the playing sound
                        sound.Frequency = 44500;
// You can change the volume on the playing sound
// 0 is conciddered to be full volume where -10000 
                        // is silence
sound.Volume = -1000;
// To loop the sound start playing
                        // with the loop option like
sound.Play(0, BufferPlayFlags.Looping);

last edited 2005-03-27 14:45:43 by JustinDaubenmire