473,503 Members | 12,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PlaySound with Resource Question

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
Nov 17 '05 #1
2 1672
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
"Joe Thompson" <Jo*********@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
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

Nov 17 '05 #2
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

"Antti Keskinen" wrote:
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
"Joe Thompson" <Jo*********@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
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


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5470
by: Ben Bateman | last post by:
I am tyring to write a small menu program that will display the lyrics of a song, and then play the song. I was told that you have to convert the mp3 to wav, did that. The 2 function calls...
2
699
by: Ángel Cabrera | last post by:
I'm using Visual Studio .NET 2003. I get a "Form1.h(242): error C2065: 'PlaySound' : undeclared identifier" error all the time, I've been looking in MSDN for a couple of hours with no luck, no...
5
2332
by: MLH | last post by:
I have a line of code that works when called from a procedure in Access 2.0 form... PlaySound("C:\cr\help\Help0018.wav", 0) I imported what I thought was needed into A97. However, running it...
3
4633
by: Harry J. Smith | last post by:
I added some sounds to my application, but the example in the msdn Library did not work. It had: public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags ); The...
4
4497
by: rcattral | last post by:
Has anybody been using the PlaySound() function to play WAV files from within a DLL? Here is what I normally do. Since upgrading to 7.1 NET after using VC 6.0 for quite some time, I found that I...
10
2449
by: Ot | last post by:
I found information on PlaySound. I implemented it in my program and it works just dandy. The only little problem is that I have to package the ..wav files and send them along since PlaySound...
9
11828
by: Andy | last post by:
Hi, I have an application that has several forms. Each form acts as a monitor of a gateway system. If the gateway appears to be dead, the application should play a .wav file (not resource)...
3
10612
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with...
0
1884
by: poppy | last post by:
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.