Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ playsound()


ghost's Avatar
0 0

Ok. I've been attempting to include sound in my project for a certain period of time. Fortuantly, I came across this:

#include <MMSystem.h>

int main() { char* WAV = "c:\\ratm-bulletinthehead"; sndPlaySound(WAV, SND_ASYNC); return 0; }

However, the compiler states that I have errors like these:

C:\DEV-C_~1\Include\MMSystem.h:896: syntax error before `;'

898 c:\dev-c_~1\include\mmsystem.h `CALLBACK' was not declared in this scope

Line 898? It say's the same thing about 14 times except with "DWORD" not declared and such. I don't know if this is because a code like this is directly related to VC++ because I am yielding way to many errors. So, I came across this:

#include <windows.h> #include <mmsystem.h> #pragma comment(lib, "winmm.lib")

int main() { PlaySound("ratm-bulletinthehead.wav", NULL, SND_FILENAME | SND_SYNC); return 0; }

Well, I got all the necessary headers I would suppose. And it compiles fine, but it can't execute ;S does anyone know a solution e.g change this to that or you just plain can't do it lol.


ghost's Avatar
0 0

Well, this works..

#include &lt;windows.h&gt;
#include &lt;mmsystem.h&gt;
#pragma comment(lib, &quot;winmm.lib&quot;)

int main(int argc, char *argv[]) 
{
	PlaySound(&quot;C:&#92;&#92;test.wav&quot;, NULL, SND_FILENAME | SND_SYNC);
	return 0;
}

so I'm going to guess that you forgot to specify the full path to your .wav file. ;)


ghost's Avatar
0 0

Lol, I even renamed my ratm song to test.wav, copied and paste and in the compiler logs I see this:

c:\dev-c++\untitled1.o(.text+0x27):untitled1.cpp: undefined reference to `PlaySoundA@12'

>: ( what compiler did you use?


ghost's Avatar
0 0

I'm using Visual Studio 2003 .NET, but try the following..

Project -> Project Options -> Paramters

In the text box labeled 'Linker', add the flag: Code:

-lwinmm


ghost's Avatar
0 0

Woot…worked thanks a lot man :)


ghost's Avatar
0 0

No problem, but you should check out msdn.com and search for their free copy of MSVC Express 2005 it will save you a lot of headaches and I think it's a lot more organized than Dev-C++, but to each his own.


ghost's Avatar
0 0

Yeah I think I will, thanks again man I was searching forever lol.