tags:

views:

744

answers:

5

i try to play a music file in my coding, but failed. i have my music file in the same folder which save .cpp file.

can someone help me?

my code is:

#include <iostream> 

#include <windows.h>

int main() { 
PlaySound("kenny g.WAV", NULL, SND_ASYNC); 

}
A: 

Can you use absolute path and check if it is path error?

Ex: PlaySound("C:\\kenny g.WAV", NULL, SND_ASYNC);
aJ
no, it doesn't work.
did you link to the required lib?
aJ
can u give me a fully work example?
+2  A: 

You need to use the absolute path, make sure that you're sending a filename (use SND_FILENAME flag), and pause the program long enough to play the sound file (e.g., use getchar()). You need to link the winmm.lib library in your project settings, and #include windows.h and mmsystem.h in the header.

#include <windows.h>
#include <mmsystem.h>

int main() {
    PlaySound((LPCSTR) "C:\\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
    getchar();
}

API: http://msdn.microsoft.com/en-us/library/ms712879%28VS.85%29.aspx
That should be it. Let me know, thanks!

Luis B
How would you compile that example under gcc/windows?
Joshua Moore
A: 
int main() { 
    PlaySound("kenny g.WAV", NULL, SND_ASYNC); 
}

With the SND_ASYNC flag your program can (and it will) terminate immediatelly!

Try PlaySound("kenny g.WAV", NULL, SND_SYNC); first to see if it works.

Nick D
it dint work..sorry
A: 

Speaking about the path, your data file should be where your executable is, not where your source file is, if the path is not absolute.

And yeah, this very question has been asked 9 years ago ;)

Ashalynd
A: 

Did you manage to fix this? I'm getting the same error...

logic-unit
Okay i fixed this. You need to download the Windows SDK, and use the winMM.lib file contained in that. in code::blocks go to project > build options > linker settings and add the link to winmm in there.it should work fine then! :-)
logic-unit