Solved

How to insert a .wav in the resource of an exe file and play it using Playsound()?

Posted on 2008-06-10
7
997 Views
Last Modified: 2013-11-17
How to insert a .wav in the resource of an exe file and play it using Playsound()?
0
Comment
Question by:Xyptilon2
  • 3
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 175 total points
ID: 21767821
Not a hard task...
In fact I wrote an article about resource files linked to an executable or inside a resource dll on http://bcbjournal.com.
So... Create a text file in notepad.
First line:
THEWAVFILE     WAV   TheWaveFile.wav

Save the file with extension .rc
Copy Brcc32.exe from bin directory to the directory of the rc file and of course the wav file....
Both files has to be present (rc and wav) or the content of the text file as file names.

Open a command prompt on that directory to use brcc to create a .res file as follows:
brcc32 -32 TheFileName.rc TheFileName.res
A .res file will be created with a size a bit greater than all the files included inside.
Add the res file to the executable using Project|Add To Project
The next time you will compile and link the wav file or whatever else will be included in the executable.

The hard part is to play the sound...
Usually I'm using DirectSound for that so all I ever needed is a buffer...
ANYWAY:
You can load the sound to a TMemoryStream as follows:
TMemoryStream   *Stream;
       HRSRC   ResHandle;
        HGLOBAL MemHandle;
        void *ResPtr;
        int   ResSize;
        char  *lpName;

        ResHandle = FindResource(HInstance, "THEWAVFILE", "WAV);
        MemHandle = LoadResource(HInstance, ResHandle);

Using the TMemoryStream->SaveToFile function you can save to a temporary file before use the playsound function if there isn't any other way.


George Tokas.
        ResPtr = LockResource(MemHandle);
        ResSize = SizeofResource(HInstance, ResHan-dle);
        Stream = new TMemoryStream;
        Stream->SetSize(ResSize);
        Stream->Seek(0,0);
        Stream->Write(ResPtr, ResSize);
        Stream->Seek(0,0);
       
0
 
LVL 25

Assisted Solution

by:kode99
kode99 earned 75 total points
ID: 21773162
PlaySound works fine you just have to tell it the 'file' is a resource as follows:  

PlaySound("THEWAVEFILE",NULL, SND_RESOURCE);

or

sndPlaySound("THEWAVEFILE", SND_RESOURCE);

You do need to include the mmsystem.h header file.

I believe rc file needs to use WAVE not just WAV.  Depending on the file path/name you might have to use "" around the file name as well,  for example if the name has any spaces you need the quotes.

So first line:
THEWAVFILE WAVE TheWaveFile.wav

You can actually just add the .rc file to the project and the CBuilder will take care of building the .res file automatically.  Either way it gets the job done.

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21777218
>>THEWAVFILE WAVE TheWaveFile.wav
Whatever can be used as long as the middle description in this line and the last variable on FindResource() is the same...
I tried that and verify it...:-)
Sometimes one needs to confuse others, like a kind of cryptography....

George Tokas.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:kode99
ID: 21781119
When doing the PlaySound/sndPlaySound methods to directly play the audio  from the resource only 'WAVE' works (using the SND_RESOURCE flag).  I would guess those MS SDK calls have hardcoded thier resource lookups.

If you load it into memory manually you are right,  anything will do as long as you  match the call to what you put in the resource file.

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21781606
>>When doing the PlaySound/sndPlaySound methods to directly play the audio  from the resource only 'WAVE' works

YES!
What I mean is to insert to and extract from resource...
I'm using that long time now, have all wav files inside a dll but I'm using DirectSound to play them...
DirectSound plays the sounds from a buffer (DirectSoundBuffer)...

George Tokas.
0
 
LVL 13

Author Closing Comment

by:Xyptilon2
ID: 31465906
Thanks guys! I got it working!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question