11

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);    
}
WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
  • What is the error code you are getting with GetErrorCode() function? – Naveen Oct 14 '09 at 10:27
  • i'm not sure about the GetErrorCode() but the error message is [Linker error]undefined reference to 'PlaySound@12' id returned 1 exit status –  Oct 14 '09 at 10:33
  • You need winmm.lib to play music as Luis mentioned – ukanth Oct 14 '09 at 10:53

9 Answers9

14

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() {
    PlaySoundA((LPCSTR) "C:\\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
    getchar();
}

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

aaronsnoswell
  • 6,051
  • 5
  • 47
  • 69
Luis B
  • 1,684
  • 3
  • 15
  • 25
7

try adding -lwinmm to your compiler settings thingy. It worked for me. Just type that in the compiler options area and it will work.

Cool Guy 2131
  • 71
  • 1
  • 1
2

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

Ex: PlaySound("C:\\kenny g.WAV", NULL, SND_ASYNC); 
aJ.
  • 34,624
  • 22
  • 86
  • 128
1
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 Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
0

you can test by PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS);

Aldee
  • 4,439
  • 10
  • 48
  • 71
0

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
  • 12,363
  • 2
  • 34
  • 37
0

Just in case it is unresolved yet! You need to include the two header files mentioned in previous comments, link the project to the required lib and place the sound file in the same folder as your .exe file (in case you are not using the full path)

Vigo
  • 707
  • 4
  • 11
  • 29
0

Try this code it works for me. Also For code::Block use winmm in linker settings.

#include <iostream>  
#include <windows.h>
#include <MMSystem.h>
 int main(){
PlaySound(TEXT("your file path.wav") , NULL , SND_SYNC) ;
    return 0;
}
M.habib
  • 1
  • 2
0

I faced the same problem. All what they say is correct but it won't play unless you removed the spaces in the file name. make it just like test.wav and it will work.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '23 at 12:27