Solved

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

Posted on 2008-06-10
7
947 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now