Windows API - Sound Playback
You can play sounds using the Windows API quite simply. The call is:
BOOL PlaySound( LPCSTR pszSound, HMODULE hmod, DWORD fdwSound );
Note: in order to use this function you must include the library header: mmsystem.h and link your project to winmm.lib (the multi media library).
- pszSound - this is the filename of the sound you wish to play, e.g. "explosion.wav". It can also be a loaded resource.
- hmod - this is always NULL if the above is a filename
- fdwSound - this is a combination of flags determining the meaning of the other parameters and some options on playing a sound. e.g. specifying SND_FILENAME | SND_ASYNC means pszSound is a filename and that the sound should be played asynchronously (the PlaySound call will exit immediately) .For other flags look in the MSDN help file.
The function returns true if it was successful or false if it failed. If it fails it is often because the sound file has not been found.
To play an explosion that loops we could write:
PlaySound("explosion.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
Only one sound can play at once so if you play another sound before the first is finished it will be cut off. You can also stop a sound playing by passing NULL as the pszSound parameter.
For more information on PlaySound see the MSDN page here: The PlaySound function