Home Articles Books Downloads FAQs Tips

Q: Load and play wave sound from a program's resources


Answer:

The FAQ titled "adding icons, cursors, and bitmaps to a BCB project" contained an example of how to use RC files in a BCB project. If you look closely, you will see that the RC file adds a WAVE resource to the project. The WAVE resource gets bound to the executable, just like a bitmap or an icon. This FAQ demonstrates how you can play the wave resource at runtime.

The API provides a function called PlaySound that allows you to play a wave soundbite. PlaySound does not provide any visual feedback that the audio is playing, it simply plays the sound, and that's it. This makes PlaySound a good choice for playing short wave resources that you associate with an action in your program. The code example below uses PlaySound to play a typewriter sound whenever the user types in a TMemo control (The WAV file just happens to be one that I just happened to have laying around on my PC. If you don't have TYPE.WAV, use the file TICK.WAV that comes with BCB's football example)

Step 1:

Add the wave resource to your project. The FAQ on resources covers this in detail. The code below summarizes the resource statements that you should use to compile this example. Create an RC and an RH file that contain these statements, and add the RC file to your BCB project.

  // insert this line in an RC file
  IDW_TYPE     WAVE     "type.wav"
  // insert this line in an RH file
  #define IDW_TYPE     1000
Step 2:

Use the PlaySound API function to play the wave resource (to use the code below, place a TMemo on a form, and create an OnKeyPress handler).

#include <mmsystem.h>
#include "res.rh"

void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
    PlaySound(MAKEINTRESOURCE(IDW_TYPE),
              HInstance,
              SND_RESOURCE | SND_ASYNC );
}

Note: SND_RESOURCE tells Windows that the first parameter to PlaySound is the resource name of a wave resource that is bound to the executable. Change this parameter to SND_FILENAME if you need to load a wave file. When loading files, the first argument to PlaySound is a char * the contains the filename. You can also play system sounds by using the SND_ALIAS. Here are some examples of SND_FILENAME and SND_ALIAS.

PlaySound("type.wav",   NULL, SND_FILENAME | SND_ASYNC |SND_NOSTOP);
PlaySound("SystemStart",NULL, SND_ALIAS    | SND_ASYNC |SND_NOSTOP);

For a list of available SND_ALIAS associated sounds, look in the registry under HKEY_CURRENT_USER\AppEvents\Schemes

Note: The SND_ASYNC flag causes PlaySound to return before the sound has finished playing. Specify the SND_SYNC if you prefer that the wave sound finish before the returns.

Note: The SND_NOSTOP flag prevents the sound from being interrupted from another sound request. To see the flag in action, test the typewrite sound with and without the flag.

Note: There are other flags that you can pass to PlaySound. For more details, consult the Microsoft Multimedia help file that comes with C++Builder.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.