Playing sound from resource

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
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 ).

closed account (NUCkSL3A)
Ok, could I see what that would look like? I don't know there the .rc file is.
Last edited on
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.

closed account (NUCkSL3A)
Alright, I will check this out and see if it will work.
closed account (NUCkSL3A)
Just wondering, in what folder should it be?
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
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?
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).
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?
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.
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
Topic archived. No new replies allowed.