?
Solved

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

Posted on 2008-06-10
7
Medium Priority
?
1,039 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 700 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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