So how do I play sounds anyway?

Well, the answer to that is WAY more complicated than I can answer, there are LOTs of different ways, and I’m not qualified to talk about most of them.

But I can talk about some of the easier ways to make noises.

The first, and simplest one is PlaySound (and its little brother sndPlaySound).

The PlaySound API takes a WAV file (or resource) and plays its contents.  A WAV file is essentially raw PCM data with additional information describing the sample size and the sample rate (there’s more to it, but that works).

So to play a “ding”, you can just call:

        PlaySound(“ding.wav”, NULL, SND_FILENAME);

That’s it, the “ding” sound will play on your speakers.

But that’s not all you can do with PlaySound.  You can embed a resource in your executable and play that resource by specifying SND_RESOURCE – in that case, the sound name (ding.wav in the example above) is a resource ID and that resource will be extracted and treated as a WAV file and played.

In addition, you can use PlaySound to play sounds that are controlled by the sound themes control panel applet.  All versions of Windows support a set of default events (SystemAsterisk, SystemExit, SystemStart, etc) that can be played by simply calling:

        PlaySound(“SystemAsterisk”, NULL, SND_ALIAS);

This will play whatever sound is set for the “Asterisk”.

PlaySound can also be used to play sounds looped (with SND_LOOP), you can have it play asynchronously (SND_ASYNC), etc.

One important caveat with SND_ASYNC – SND_ASYNC means that the playback of the sound file is asynchronous.  But before the sound can be played, the sound needs to be loaded into memory, which involves loading resources, opening files, etc, and all of these can take some time to complete.

If all you need to do for your app is to make dings and whatnot, PlaySound is actually pretty cool, and remarkably lightweight.