Play Sound Sample
The Play Sound sample shows how to play a WAV file in a DirectSound secondary buffer.
Source: (SDK root)\Samples\C++\DirectSound\PlaySound
Executable: (SDK root)\Samples\C++\DirectSound\Bin
Load a WAV file by clicking Sound File. Select Loop Sound if you want it to play repeatedly. Click Play.
The sample shows the basic tasks required to play a sound in a DirectSound Buffer, as follows:
Set up DirectSound:
- Call the DirectSoundCreate function to create the DirectSound object.
- Call IDirectSound8::SetCooperativeLevel.
- Set the primary buffer format. This sample calls the DSUtil_SetPrimaryBufferFormat function in Dsutil.cpp to do this.
Load a WAV file into a DirectSound buffer:
- Read the WAV file header to get the data size and format.
- If the WAV file is a reasonable size, create a secondary buffer in the same format as the data, large enough to hold the entire file. If the WAV file is too large, data will have to be streamed to the buffer. See Stream Data Sample.
- Fill the buffer with data. Obtain a pointer into the buffer by calling IDirectSoundBuffer8::Lock. Because data is not being streamed as the buffer is playing, the entire buffer can be locked. After the memory has been copied, call IDirectSoundBuffer8::Unlock.
Play the DirectSound buffer:
- Check to see if the buffer has been lost. If so, restore it.
- To play the buffer call IDirectSoundBuffer8::Play.
Call Release on all the DirectSound interfaces that were obtained. Release buffers before releasing the DirectSound object.