Play Sound Sample

The Play Sound sample shows how to play a WAV file in a DirectSound secondary buffer.

Path

Source: (SDK root)\Samples\C++\DirectSound\PlaySound

Executable: (SDK root)\Samples\C++\DirectSound\Bin

User's Guide

Load a WAV 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:

  1. Call the DirectSoundCreate function to create the DirectSound object.
  2. Call IDirectSound8::SetCooperativeLevel.
  3. Set the primary buffer format. This sample calls the SetPrimaryBufferFormat function in DXUTsound.cpp to do this.

Load a WAV file into a DirectSound buffer:

  1. Read the WAV file header to get the data size and format.
  2. 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.
  3. 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:

  1. Check to see if the buffer has been lost. If so, restore it.
  2. 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