Playing a WAV from a resource.

by Sep 24, 1997

 Question and Answer Database

FAQ498C.txt   Playing a WAV from a resource.
Category   :Windows API
Platform    :All
Product    :C++Builder  1.x

How do I play a sound from a resource?

We haven't been able to get the SND_RESOURCE flag to work, however
you _can_ load up the resource into memory and then use the 
SND_MEMORY flag.  Follow these easy steps:

1. Load a .wav file in to your applications .rc file by selecting
   Add resource ... choosing a RC_DATA type and grabbing a .wav 
   file in the open dialog. Note what the resource Identifier # is
   as this is waht we will use to identify the resource. It is usually
   the number in parens after the name of the resource. For example
   RCDATA_1 (1), 1 would be the resource id.

2. Declare a pointer that you can access from the function which 
   you wish to play the sound.

   LPCSTR lpWavInMemory;

3. Use the following WinAPI stlye series of function calls to 
   load the sound into memory:

   lpWavInMemory = (LPCSTR)LockResource(
                           FindResource(NULL, "#1", RT_RCDATA)));

   As LockResource returns a LPVOID and PlaySound wants a LPCSTR
   we need to type cast the return.

   The "#1" refers to the resource number you are using, if you
   were using resource id 2 it would be "#2".

4. To play the sound, use the following syntax:

   PlaySound(lp, NULL, SND_MEMORY);

7/2/98 10:32:32 AM

Article originally contributed by Alan Ellis