Playing a sound from a resource file.

Greetings all,

I understand how to play a .wav file in a program, but I am attempting to "package" the .wav file with the project so that it can be played by another person, on a different machine (namely, my professor). The way I have come to understand this is that if you use the following line of code:

PlaySound(TEXT("C:\\Windows\\Media\\notify.wav"), NULL, SND_FILENAME);

The sound file would not play if it is not in the same location on the other computer. Am I correct? Assuming that I am, I have added the file that I wish to play as a resource in my project, but I can't figure out how to make it play. Looking at MSDN (and other resources) I found the following snippet:

1
2
3
4
5
6
#define MAKEINTRESOURCE

PlaySound(
MAKEINTRESOURCE(L"notify.wav"),
GetModuleHandle(NULL),
SND_RESOURCE);


I get no errors when I build the program, but no sound plays when I run it. Any help here would be greatly appreciated. I have tried for hours now to figure this out (which is usually enough time for me to sort through the problem) but I have met with no success. This leads me to believe I am either completely off in my assumptions, or I am making a silly mistake.

Thanks in advance.
You don't need to turn the sound into a resource. You can simply supply the sound file in the local project directory (the directory in which the executable is contained), and then do something to the following effect:

PlaySoundW("notify.wav", NULL, SND_FILENAME | SND_NODEFAULT);

From the MSDN documentation:

PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. If the function cannot find the specified sound and the SND_NODEFAULT flag is not specified, PlaySound uses the default system event sound instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.


http://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx
Thanks so much for the info, xismn. It worked like a charm, the only thing I had to add to the code is the "L" before the sound, like this:

PlaySoundW(L"pow.wav", NULL, SND_FILENAME | SND_NODEFAULT | SND_ASYNC);

Without the "L" there was an error. I have a vague understanding of using the "L" as I learned it was also necessary in the MessageBox() that I have used. I need to read up a bit on why this is so - as I don't fully grasp it - but it works.

Again, thanks for your time.
No problem! Glad it worked out.
The reason is because you are calling PlaySoundW(), which expected a wide string for the file name.

Some interesting reading related to this:
http://www.cplusplus.com/articles/2w6AC542/
Thanks for the link, Zhuge. Very informative...
Woops, silly me. PlaySoundW was my suggestion :v
Topic archived. No new replies allowed.