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

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

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

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

Related posts

Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64


C++Builder and Platforms Support


What's New in the GetIt Package Manager - June 2020


RAD Studio 10.4 Now Available, Learn More

Leave a Reply

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