Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

Playing a WAV from a resource.

Author: Embarcadero USA

 Question and Answer Database

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

Question:
How do I play a sound from a resource?

Answer:
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(
                           LoadResource(NULL, 
                           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

RAD Studio 13.2 Florence Now Available! Kai 1.1.1 Now Available! What's Coming in RAD Studio 13.2 Florence

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES