Direct Sound: Playing a Sound


Covers: Creator SDK
Topics: In this tutorial you will lern to initialize DirectSound and Play a wav file.

First load a new project and add reference to DirectX7, then Declare the variables, here is the code:

'Variables of Direct Sound Buffer
'and Direct SOund Buffer Description.
Dim sb As DirectSoundBuffer
Dim sbd As DSBUFFERDESC
Dim wf As WAVEFORMATEX

'Direct Sound and DirectX Variables
Dim ds As DirectSound
Dim dx7 As New DirectX7

Private Sub Form_Load()
'Initialize and set cooperative level of DirectSound
Set ds = dx7.DirectSoundCreate("")
ds.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL

'Set Buffer Description
sbd.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME
wf.nSize = LenB(WAVEFORMATEX)
wf.nFormatTag = WAVE_FORMAT_PCM
wf.nChannels = 2
wf.lSamplesPerSec = 44000
wf.nBitsPerSample = 16
wf.nBlockAlign = wf.nBitsPerSample / 8 * wf.nChannels
wf.lAvgBytesPerSec = wf.lSamplesPerSec * wf.nBlockAlign

'Create a Sound Buffer
Set sb = ds.CreateSoundBufferFromFile("Sample.wav", sbd, wf)

'Play Sound Buffer
sb.Play DSBPLAY_DEFAULT
End Sub
Hosted by www.Geocities.ws

1