PlaySound with Resource Question

G

Guest

Hi,

I am trying to use PlaySound in a VC++.net Windows app (VS 2003). I can use
it to play a file but now I want to play it from a resource. I have two
questions:

How do I add a wav file to my project as a resource (steps please)

How do I call PlaySound once that I have the wav file as part of the resource.

Thank you,
Joe
 
A

Antti Keskinen

Hello !

This is more simpler than you might imagine.

Your first task is to add the actual wave file into your project as a
resource. To accomplish this, the wave file should be in standard PCM wave
format. Now proceed with the following steps:

1. Go to the solution view of Visual Studio, and click on the project name.
Then right-click, choose 'Add -> Add Resource' from the pop-up menu
2. In the "Add Resource" dialog that opens, click on the 'Import' button.
3. This will open a file browser. Now, change file types to view to 'All
files (*.*)'.
4. Browse to the directory where your audio file is stored, click on it's
name and choose 'Open'.
5. A dialog box is displayed, asking for the resource name. Use whatever ID,
such as IDR_MYWAVEFILE.
6. The resource is now added

In order to play back the resource file, you use PlaySound directly. First,
make sure that the compilation unit (.cpp file) that is supposed to play the
sound has a) included "resource.h" and b) has some means to get the instance
handle of the executable. This is the handle passed to WinMain when your
program starts. In MFC, you can use AfxGetResourceHandle() to obtain this
handle.

When these pre-steps are taken care of, playing the sound is a matter of
calling:

PlaySound( MAKEINTRESOURCE(IDR_MYWAVEFILE), <instance handle here>,
SND_RESOURCE );

You should hear the sound play through once. If you wish to loop it, use the
necessary flags for PlaySound, just as if you were playing back a file.

Hope this helps.

-Antti Keskinen
 
G

Guest

Hi Antti,

This is exactly what I was looking for - thank you for taking the time to
explain it so well. I am using NULL for the Handle and it seems to work.

Thank you,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top