PlaySound
Description
The PlaySound sample shows how to play a wave file in a DirectSound secondary buffer.
Path
Source: (SDK root)\Samples\Multimedia\DirectSound\PlaySound
Executable: (SDK root)\Samples\Multimedia\DirectSound\Bin
User's Guide
Load a wave file by clicking Sound File. Select Loop Sound if you want it to play repeatedly. Click Play.
Programming Notes
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 wave file into a DirectSound buffer:
- Read the wave file header to get the data size and format.
- If the wave 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 wave file is too large, data will have to be streamed to the buffer. See the StreamData 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.
Free DirectSound:
Call Release on all the DirectSound interfaces that were obtained. Release buffers before releasing the DirectSound object.
See Also