Play wav sound on Pocket PC

Vassili Philippov (vasja@spbteam.com), August 23, 2001.

Introduction

There are several functions on Pocket PC that can be used for playing sound in your program. These are: PlaySound, MessageBeep, waveOutWrite and other waveOut... functions.

This article covers working with PlaySound function. Using waveOut... functions is described in the Playing audio on the Pocket PC article.

What You Need

PlaySound

It's easy to play sound using PlaySound function. Let's start with an example. The following code plays empty.wav files in \Windows folder. If your \Windows folder doesn't contain this file then copy any .wav file and rename it to empty.wav.

PlaySound(_T("\\Windows\\empty.wav"), NULL, SND_ASYNC | SND_FILENAME);

PlaySound function can play .wav file from different sources. In the example above it plays it from a file. It can also play a file using data in memory and in application resources. This function can also be used for playing system sounds.

There are two modes of playing sound synchronous and asynchronous. Use SND_SYNC and SND_ASYNC values in flag parameter to choose a necessary mode.

Playing .wav files using PlaySound

Example:

PlaySound(_T("\\Windows\\empty.wav"), NULL, SND_ASYNC | SND_FILENAME);

Playing .wav file you should remember that Pocket PC doesn't support relative file names. So you should always provide the full path to the file.

Playing .wav resources using PlaySound

Playing sound from a resource can be convenient if your application has a fixed number of sounds to play. Using resources instead of files allows you to avoid many .wav files that can be extremely helpful if you are going to create an application that consists of one executable file.

First of all you should create .wav resources. Create a custom resource:
Create new custom resource

enter WAVE resource type:
Enter WAVE resource type

choose the resource name and file where the sound will be stored:
Choose resource name and .wav file where the sound will be stored

Then copy necessary .wav file into testsound.wav and you could use this resource like:

PlaySound (_T("testSound"), ::AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);

Play .wav data from memory using PlaySound

Sometimes it's necessary to play sounds when the content of .wav file is stored in memory. I used to meet two situations: one when I received a file using network and another when I read a .wav file from compound document. The content of the .wav file should be located as one piece of memory. An example below reads the file into memory (assuming that the file size is less then 100Kb) and then plays sound from memory:

byte buffer[100*1024]; CFile file; file.Open(_T("\\Windows\\empty.wav"), CFile::modeRead); file.Read(buffer, 100*1024); file.Close(); PlaySound((const unsigned short *)buffer, NULL, SND_MEMORY | SND_ASYNC);

Conclusion

PlaySound function is suitable when your sound is in .wav format. You could add some .wav sounds to resources that will allow you to avoid additional .wav files in an application.

Related resources: