473,738 Members | 6,042 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 1685
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
5481
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 PlaySound function works correctly, but it is not playing the wav file.
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
2349
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
4640
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
4510
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
2479
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 be in the registry and actually controlled/selected by the end user.
9
11842
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 sndPlaySound function, with the code:
3
10640
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 audioRes) { // Get the resource into a stream
0
1900
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, in order for this example to work, you should make sure that you have such a WAVE file on your...
0
9473
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9259
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.