Platform SDK: DirectX

Step 3: Play the Sound

[C++]

This section pertains only to DirectX for Visual Basic. See DirectSound C/C++ Tutorials.

[Visual Basic]

In the Tutorial 1 sample, the sound is loaded onto a sound buffer when the user clicks the Play button. At the same time, the state of the Volume and Pan sliders is checked by calling their Scroll procedures, and the properties of the sound buffer are adjusted accordingly.

Private Sub scrlVol_Scroll(i As Integer)
    If m_dsBuffer(i) Is Nothing Then Exit Sub
    m_dsBuffer(i).SetVolume scrlVol(i).Value
End Sub
 
Private Sub scrlPan_Scroll(i As Integer)
    If m_dsBuffer(i) Is Nothing Then Exit Sub
    m_dsBuffer(i).SetPan scrlPan(i).Value
End Sub

These procedures, and the corresponding Change procedures, are called whenever the user adjusts the volume and pan sliders. The application can make changes to the volume and pan as the buffer is playing.

The sample also checks the state of the Loop Play checkbox and sets a flag to 1, or DSBPLAY_LOOPING, if it is checked.

Dim flag As Long
flag = 0
If chLoop(i).Value <> 0 Then flag = 1
 

This flag is passed to the DirectSoundBuffer.Play method, as follows:

m_dsBuffer(i).Play flag

If the sound is looping, the user can stop it by clicking Stop or by clearing Loop Play. In response, the application stops the buffer and resets the current play position to the beginning.

m_dsBuffer(i).Stop
m_dsBuffer(i).SetCurrentPosition 0

If the user clicks Pause, the buffer is stopped but the current play position is left where it is, so the sound will resume at the same place when DirectSoundBuffer.Play is next called.