473,545 Members | 2,678 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 1677
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 AfxGetResourceH andle() 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*********@di scussions.micro soft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.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 AfxGetResourceH andle() 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*********@di scussions.micro soft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.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
5475
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 under each case is included in another header file that I created. I have the <mmsystem.h> included in the main cpp file. The functions under the...
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 idea, it's my first program, if you are so kind and could explain me in detail. Sorry for my english. Regards.
5
2336
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 is unsuccessful. I get an error saying "file mmsystem not found". I think I've violated some API call protocol.
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 file coredll was not found. I changed it to and it now works. What is the best way to handle this? -Harry
4
4503
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 had to add the following lines in order to use the multimedia extensions. In stdafx.h #include <MMSystem.h>
10
2457
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 plays a sound file. One of the options I found in a nice description of PlaySound says that there are a couple of other choices. 1) The sound can...
9
11833
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) until the user clicks a button to acknowledge the situation. To keep things simple, lets assume there is only one monitor. If I use the old...
3
10619
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 Windows, and they play fine outside of my app. This code works, but has the random "scratching" problem: private static void PlayResource(string...
0
1890
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 help. /* This uses the high level WAVE API PlaySound() to play a WAVE file in memory. I simply open the WAVE file called "nagelas.wav" (ie,...
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5354
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1039
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.