Written by JustinDaubenmire
This tutorial shows you the basic startup tasks to play a wav file using DirectX version 9 and vb.net. It assumes the following:
you are using Visual Studio 2003
you have the DirectX version 9 SDK installed.
You have a sound file named sound.wav in the bin folder of your project
If you do not have the DirectX 9 SDK installed on your computer, prior to trying this source code, please download
The DirectX 9 SDK and install it.
Ok, lets get started. Open up Visual Studio 2003 and start a new vb.net windows application project and name it PlaySound.
Once the project appears, you'll see that it has Form1 in the solution explorer. Press enter on form1 to open it up and 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 and add this imports statement to the top of form1 above the line Public Class Form1:
Imports Microsoft.DirectX.DirectSound
Now in the form1 class, just under the text "#Region Windows Form Designer generated code" add these variables...
' sound card device
Private oDev As Device = Nothing
' sound buffer description
Private oBufferDescription As BufferDescription = Nothing
' holds sound.wav file data
Private oSound As SecondaryBuffer = Nothing
Then in the form load event for form1, add this code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' setup sound card
oDev = New Device
oDev.SetCooperativeLevel(Me, CooperativeLevel.Priority)
' setp buffer description
oBufferDescription = New BufferDescription
' do we want to control frequency?
oBufferDescription.ControlFrequency = True
' do we want to control pan?
oBufferDescription.ControlPan = True
' do we want to control volume?
oBufferDescription.ControlVolume = True
' do we want our sound to play even when our app
' doesn't have focus?
oBufferDescription.GlobalFocus = True
' load the sound effect into the buffer
oSound = New SecondaryBuffer(Application.StartupPath & "\sound.wav", oBufferDescription, oDev)
End Sub
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 click sub. Here is the code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' play the sound
oSound.Play(0, BufferPlayFlags.Default)
End Sub
Here are some more things you'll want to know when it comes to playing sounds:
' to stop a sound you use
oSound.Stop()
oSound.SetCurrentPosition(0)
' to pause a sound you use
oSound.Stop()
' to pan a sound you use
oSound.Pan = -3000
' or...
oSound.Pan = 3000
' to play a sound looping you use:
oSound.Play(0, BufferPlayFlags.Looping)
' to adjust volume you use:
oSound.Volume = -300
' or...
oSound.Volume = 0
' to adjust frequency you use:
oSound.Frequency = 30000