Microsoft DirectX 8.1 (C++)

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:

  1. Call the DirectSoundCreate function to create the DirectSound object.
  2. Call IDirectSound8::SetCooperativeLevel.
  3. 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:

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