Playing Wave Files Directly From The Resource via DirectSound

Public Interface

Introduction (back to top)

This class is a "spin off" of the CGTetris application. The CDirectSound class makes it easy to play a WAVE file directly from the resource (without copying it into a temporary file) via Microsoft's DirectX component DirectSound. The class is designed to play small sounds, that can be placed completely into memory. No streaming is realized in this implementation. One can use the class to play WAVE files, too. In this case you have to load the entire file into memory and to pass the pointer to the beginning of the buffer to the Create() method. To use this class you need at least DirectX version 3.

Public Interface (back to top)

// create the sound object. You cannot use a MIDI object until
// you "Create()" one!
BOOL Create(LPVOID pSoundData, DWORD dwSize, CWnd * pParent = 0);
BOOL Create(LPCTSTR pszResID, CWnd * pParent = 0);
BOOL Create(UINT uResID, CWnd * pParent = 0);

// Play the sound file. Normally the playback will stop after
// the sound played off.
BOOL Play(DWORD dwStartPosition = 0, BOOL bInfinite = FALSE);

BOOL Stop();
BOOL Pause();
BOOL Continue();

// Enable/disable the sound object. If you call Play() on a
// disabled sound object, nothing will happen. This makes
// it somewhat easier to globally switch off sounds in an
// application.
BOOL EnableSound(BOOL bEnable = TRUE);
BOOL IsEnabled() const;

Usage (back to top)

The source consists of 2 files:

Add a WAVE resource into your application (from the Menu Insert->Resource->import->*.wav).
"Create()" your CDirectSound (derived) object giving it the resource-id of your WAVE resource. Now you can call Play(), Pause(), Stop() etc.

Download(back to top)

Download demo project - 5 KB

Download source - 71 KB