Solved

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

Posted on 2008-06-10
7
1,019 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 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 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