Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help beginner play sound from a resource 1

Status
Not open for further replies.

serpento

Programmer
Jun 16, 2002
92
0
0
GB
I want to play a sound called chomp.wav that is stored as a resource. I found:
[tt]
PlaySound("SoundName", hInst, SND_RESOURCE | SND_ASYNC);
[/tt]
on MSDN but when I try and compile this it says hInst is undeclared. What do I have to do to hInst?.

Also, I haven't used resources before. All I've done so far is made chomp.wav be under "resource files" in the file veiw but I don't know if this is the right thing to do at all. Surely I have to call it "SoundName" or something?

Any help would be much appreciated, -Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
you need include the appropriate import library to your project.
If under Visual C++, choose the "Project->Settings" menu option, and hit the "Link" tab option. CHoose "General" from the combo box menu in the dialog. You'll see a text field called "Object/Library Modules".

In there add "Winmm.lib" (without the quote marks!) - ensure it is entered for both debug and release models.

To play the sound resource:

[tt]PlaySound(MAKEINTRESOURCE(resID),
GetModuleHandle(NULL),SND_RESOURCE);[/tt]

where 'resID' is the numeric resource identifier for your sound!

[deejay]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Thanks! . . . but it doesn't seem to work :-( .

I've imported my chomp.wav in as a resource and now my resource file says:
[tt]
chomp WAVE DISCARDABLE "Debug\\chomp.wav"
[/tt]
I then inserted this code into my program:
[tt]
PlaySound(MAKEINTRESOURCE("chomp"), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
[/tt]
an adaptation of what you suggested, but I hear no sound when I run it. It works when I play it straight from the file like:
[tt]
PlaySound("chomp.wav", NULL, SND_FILENAME | SND_ASYNC);
[/tt]
but that isn't really what I want. I'm getting no compiler warnings or errors. -Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
The problem is this part:

[tt]MAKEINTRESOURCE("chomp")[/tt]

If you are using Visual C++ then you should have a resource identifier something like [tt]IDR_CHOMP[/tt] - the default resource name will be something like [tt]IDR_WAVE1[/tt] when you first import the sound as a resource.

therefore, you should rename the resource to [tt]IDR_CHOMP[/tt] and try this:

[tt]PlaySound(MAKEINTRESOURCE(IDR_CHOMP),
GetModuleHandle(NULL),SND_RESOURCE);
[/tt]

tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Thanks again,

I think it will work now, but I can't tell because Visual C++ 6 chrashes itself when I try and compile it [evil]! I think I'll have to try re-installing it. Your help is greatly appreciated as I had already spent 2 hours searching for a solution to this seemingly simple problem, yours being the most promising. -Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Always glad to help Ed... I've been using this method of playing sounds with Visual C++ 6.0 for some time now without any problems. I suspect you are correct and that you need to re-install it because it should work. We know it can't be the sound file itself because you have already tested it by playing the sound direct from the file.

I'm assuming that you don't want to use that method because you'd like the sound file neatly locked away in your app's resources. Well, if this is correct and you still having problems with it, there is still a work around....

I developed a class called "CFileExtractor" which you could use to "extract" the sound file from your application's resources at runtime. You could then use CFile::Remove() function to delete the extracted file when you're finished with it.

The class is incredibly easy to use, does everything in one line of code! All you do is supply the resource ID and the path to where you want the file extracted.

Even if you don't need to use it for this reason, you may the class useful for future projects. I personally use it to "extract" another .exe application file which updates the main application when needed - the extracted .exe downloads the update from the web, unzips it and replaces the old app!! There's loads of uses for it. You can download it at:


tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
ok i have a problem with the playsound() function, i beleive i have it syntaxed correctly,
PlaySound("beam.wav", NULL, SND_FILENAME | SND_SYNC);
and i have the wav file in the working directory and included in the project, but when i compile i get this error in the linker,
[Linker error] undefined reference to `PlaySoundA@12'
i beleive i have all the proper librarys and headers i.e.
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
and winmm.lib

and everything ive tryied does nothing and any R&D on the subject says nothing about my problem, please help

(Using Dev Cpp)
 
eShovel,

try downloading the latest Platform SDK

hope that helps
-pete
 
No you need to link to the appropriate library. Under your &quot;project&quot; menu choose &quot;settings&quot;. Click on the &quot;link&quot; tab. Ensure that &quot;General&quot; is chosen in the category combo box. You should see a text field called &quot;Object/Library Modules&quot; in the dialog. Add the following to that text field:

Winmm.lib

You'll need to do this for each configuration (ie. debug and release) you have for the project.

Recompile and run - it should now work
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
nope.
i tried what qednick said and nothin, same linker error, ima gunna try what palbano says and get the new sdkfor me BAHH!!
thx for ur help anyways guys
 
Sorry eShovel, missed the part that said you were using DevC++ [hammer]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top