Playing sound from resource

Apr 1, 2016 at 9:41pm
closed account (NUCkSL3A)
Hello! I have a problem regarding playing sound from a resource file. Of course, I could just include the sound files in the installation, but that would be too much of a hassle. So, I made this:

1
2
3
4
HANDLE inst = GetCurrentProcess();


PlaySound(L"SPOOKY.wav", HMODULE( inst ), SND_RESOURCE ); 


It gets a handle to the program, and then plays the sound "spooky.wav". At least it should.... It compiles fine, no warnings, but no sound plays. I would like to know what I'm doing wrong. Oh, and btw: the compiler shows no errors, but does show this for the output:

First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
First-chance exception at 0x77b1f697 in Application.exe: 0xC0000005: Access violation reading location 0xfffffffc.
The program '[4220] Application.exe: Native' has exited with code 0 (0x0).
Last edited on Apr 1, 2016 at 9:41pm
Apr 2, 2016 at 3:57pm
Is your sound embedded in the rsrc of your exe ?

https://msdn.microsoft.com/en-us/library/windows/desktop/dd743679%28v=vs.85%29.aspx


You might want to have a look at the excellent BASS library from IAN LUCK, it's just amazing for sounds ( and multiplatform ).

Apr 2, 2016 at 4:33pm
closed account (NUCkSL3A)
Ok, could I see what that would look like? I don't know there the .rc file is.
Last edited on Apr 2, 2016 at 4:36pm
Apr 3, 2016 at 9:56am
A resource file might look like this:
1
2
010     WAVE    "Water Drops.wav"
020     WAVE    "Creaking Door.wav"

and the C++ code to play the sound might look like this:
 
PlaySound(MAKEINTRESOURCE(020), GetModuleHandle(NULL), SND_RESOURCE);
which will play the sound 020 from the .rc file.
Though there are other possible variations in both the resource and c++ code.

Apr 3, 2016 at 3:48pm
closed account (NUCkSL3A)
Alright, I will check this out and see if it will work.
Apr 3, 2016 at 3:55pm
closed account (NUCkSL3A)
Just wondering, in what folder should it be?
Apr 3, 2016 at 4:03pm
The .rc file should be in the same folder as the .cpp file.
It probably depends which compiler and IDE you use. I just created a new project and added the .cpp and .rc files to it. The wav files will I suppose be found according to the file/pathname.
Last edited on Apr 3, 2016 at 6:24pm
Apr 5, 2016 at 3:48am
closed account (NUCkSL3A)
ok: this is what my resource file that contains the wav looks like.

IDR_WAVE1 WAVE "C:\\Users\\Vlad\\Downloads\\Wav Sound Files\\SPOOKY.wav"

How do I include that into my program?
Apr 5, 2016 at 8:50am
That depends on which compiler and IDE you are using.
Typically you just add that file to your project along with the cpp file(s).
Apr 10, 2016 at 9:55pm
closed account (NUCkSL3A)
Well, I figured it out. However, it always says "'IDR_WAVE1': undeclared identifier"
Also, I was wondering, my version of Visual Studio is from 2008. Could that be it? That it's simply outdated?
Apr 11, 2016 at 8:48am
I doubt that is has anything to do with your VS 2008.
"'IDR_WAVE1': undeclared identifier"

This sounds more that you haven't included the header files where 'IDR_WAVE1 is declared.
Apr 11, 2016 at 11:28am
I don't have the Visual Studio compiler of any vintage, I'm using gcc.
These are the steps:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>
#include <mmsystem.h>

int main()
 {
    std::cout << "\nStart\n";

    PlaySound("IDR_WAVE1", GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);

    std::cout << "\nDone\n";

    std::cin.get();
}

sounds.rc
 
IDR_WAVE1 WAVE "C:\\Users\\Vlad\\Downloads\\Wav Sound Files\\SPOOKY.wav"

Step 1. Compile the cpp file.
 
g++.exe -c main.cpp -o main.o -m32
Output from step 1 is file main.o, size 2kb.

Step 2, build the resources.
 
windres.exe -i sounds.rc -o sounds.res -F pe-i386 -O coff
Output from step 2 is file sounds.res, size is 8547 kb. This large file contains the wave data. (In this case it is almost the same size as the sound file spooky.wav).

Step 3, linking.
 
g++.exe main.o sounds.res -o Sound_Spooky.exe -static-libgcc -lwinmm -m32
Output from step 3 is file Sound_Spooky.exe, size is 9836 kb.

Run the program Sound_Spooky.exe and it plays the sound. Press enter to exit.
Last edited on Apr 11, 2016 at 12:15pm
Topic archived. No new replies allowed.