HomeХобби и стильRelated VideosMore From: profgustin

C++ Play Sound on Windows

367 ratings | 83266 views
Demonstrates setting up a Visual C++ project file to play a sound.
Html code for embedding videos on your blog
Text Comments (118)
Farheen Asif (3 months ago)
my program runs but no sound i can hear
Tackeled Gamer (3 months ago)
You are so slow UGH!
shakshak shakshak (3 months ago)
what about another format
Brannon Skinner-Dooley (4 months ago)
Blasphemy!! How dare you use system("pause")!!!
Sensu Rusu (6 months ago)
can't seem to make it work even though I've included the winmm library and the other requirements such as the audio file being in a .wav format but it just plays like the default system sound or something.
vidaoTime (7 months ago)
doesnt work for me i just get a billion errors about illegar reference to PlaySound but my imports are the same ive been looking for 2 hours and i cant find anything that helps or works all the documentation is crap is coding really this gay? everything is so vague and almost secretive like everyone but me is in on some sick joke ive been coding for years but i ant make anything good because i run into this crap
DasGandlaf (9 months ago)
I've seen this video multiple times from start to finish, added the library winmm.lib, included <Windows.h> and <MMSystem.h>, and I get a LNK2019 error. I could copy and paste that error, but my program is on german, so it wouldn't help. In the first " " there is "_imp_PlaySoundA" and in the second one is "main". Can somebody please help me ?
Usama Iftikhar Butt (10 months ago)
Sis this code works well. But the the problem is that when the audio file end then compiler perform other code. But i want the audio will play in back ground and it will not disturb other code compilation. Please answer me.
Usama Iftikhar Butt (10 months ago)
PLEASE MAKE A VIDEO ON HOW TO ADD GRAPHICS IN VISUAL STUDIO
Usama Iftikhar Butt (10 months ago)
THANK U
RiorXD (10 months ago)
>..> i literally was like why cant i play ish and it kept giving me errors and people online where just like use this code. no one said anything about adding the winmm so you saved me a headache.
skudnu (1 year ago)
using namespace std in global scope.. bad coding practice.
vici2000progame (1 year ago)
horrible quality
omar salah (1 year ago)
thanks a lot ^_^
Blekie NoMoreGames (1 year ago)
finally fuk yeah sometimes comments work more than the video PlaySound(TEXT("\\proba123\\class testing\\class testing\\Game V1\\Game V1\\bb.wav"), NULL, SND_SYNC); i had to fuking write every sub folder
Royals (1 year ago)
2:30 AHHHHHHHHHHHHH MOSKITO EVERYWHERE AHHHHHHHHHHHHHHHHHHHHHHH
Floyd (1 year ago)
remember the file has to be a .wav file.
Simon Pasi (1 year ago)
Nice! Thanks a lot.
Kubi 0040 (1 year ago)
I get 'NO such directory' error (I have also tried to just write where my file is) and my cpp file is NOT IN PROJECT
Piotr Kopciewicz (1 year ago)
That video saved me several hours :)
Awesome Channel (1 year ago)
i love you
Christian Gewert (1 year ago)
Including Windows.h will include Mmsystem.h : including Mmsystem.h manually is not needed if Windows.h is already included.
Altair 256 (1 year ago)
Thanks A LOT!!! if somebody can't see : Winmm.lib in linker :)
Minh Lê đức (10 months ago)
Nguyen Duong vl ông thần việt nam hả ;)) trg nào ng ae ;))
Dương Nguyễn (10 months ago)
thank you :3
Minh Lê đức (10 months ago)
tks man :)
Muratcan İNCE (1 year ago)
Hello I'am c++ video code watch
Eng Richard Balboa (2 years ago)
PlaySound(TEXT("C:\\Test1.wav"), NULL,SND_FILENAME|SND_LOOP|SND_ASYNC); is for loop the sound.
Jorge Alejandro B. (2 years ago)
1 question: after music I can not write... why?
Ktoś Nieznany (1 year ago)
Because this stop the program until it end playing, that's console!
aymeri baldelli (2 years ago)
thx!!!!!!!
Siscom Team (2 years ago)
How can i record my voice with DEV cpp and get data each time ???
Qayxeerll The Nine-th (2 years ago)
i have successfully play a music.. the question is how am i suppose to stop it? i want to play 2 differ music at 2 differ scene
Christian Gewert (1 year ago)
As you can read in the Win API Documentation: "If this ...(the sound file/resource)... parameter is NULL, any currently playing waveform sound is stopped. What it means is that you call PlaySound again with NULL parameters : PlaySound(NULL, NULL, 0); That will stop ANY sound playing in the current process.
Carlos Paredes (2 years ago)
I think same
klmn o (2 years ago)
tips: if you want it to work in release, add aditional dependencies in release aswell. If you want it to always be found in other computers but don't know how to find the name of it and implement it, here's the source: char username[UNLEN + 1]; DWORD username_len = UNLEN + 1; GetUserName(username, &username_len); //get the name of current pc const size_t concatenated_size = 256; char concatenated[concatenated_size]; snprintf(concatenated, concatenated_size, "C:\\Users\\%s\\Desktop\\Folder\\Extra\\Status_success.wav", username); PlaySound(TEXT(concatenated), NULL, SND_SYNC); you also have to include: #include <Lmcons.h> #include <Windows.h>
bbiero (2 years ago)
thank you,very much
RYTP MAN (2 years ago)
Качество дибильное
sdtrent (2 years ago)
Can you show us how to do this in Xcode?
Farah Lawi (2 years ago)
it works!! tq
Roscovanul2 (2 years ago)
it doesn't work it tell s me this :( Error 2 error LNK1120: 1 unresolved externals C:\Users\octav.ionescu\Documents\Visual Studio 2013\Projects\Project12\Debug\Project12.exe Project12 and Error 1 error LNK2019: unresolved external symbol [email protected] referenced in function _main C:\Users\octav.ionescu\Documents\Visual Studio 2013\Projects\Project12\Project12\Source.obj Project12
Christian Gewert (1 year ago)
It simply means that you have to link against Winmm.lib: Either include Winmm.lib in your linker settings or (if compiling with Visual Studio) use #pragma comment(lib,"Winmm.lib") within your source code to let the preprocessor automatically link Winmm.lib
Edward Puttbrese (2 years ago)
+Roscovanul2 make sure you rewatch the part about the input file. It should be at the end under the linker section.
Roscovanul2 (2 years ago)
+Roscovanul2 And it did the linking process and included the libraries
BlazeUa (2 years ago)
You are the best!
gimpyjosh (3 years ago)
Thank you so much! I was having a real issue getting this going. I was missing the link to the library. Thank you for making this.
Luka Lukšić (3 years ago)
Thanks for the video, but I need to know how play multiple sounds at the same time, can you do a video on that? PlaySound() doesn't work for that
Luka Lukšić (3 years ago)
+GuillinCorp Can you elaborate, please?
Podolski (3 years ago)
+Luka Lukšić The only thing the function does is play the sound. You might want to take a look into multi-threading. Each thread handles each function call so they can be played at the same time not having to stop the one that's currently playing.
Szymon Wróblewski (3 years ago)
why no one use code bolcks, but some shit can someone tell me how can i make it in code blocks?
Qayxeerll The Nine-th (2 years ago)
+Szymon Wróblewski might so, always buggy especially the linker part. ..but what u gonna do when its the college requirement
Szymon Wróblewski (2 years ago)
still dev sucks
Qayxeerll The Nine-th (2 years ago)
somehow i always read code block as cock blocks.. :O
Szymon Wróblewski (2 years ago)
i forgot it now xD
Diogo Ribeiro (2 years ago)
Why not share it then?
Sky Mage (3 years ago)
I can't get it to work...Why is it not working? It will lauch the application but it will only play a system sound "ting" (W7). Did I do something wrong? I did just as you did....
Saurav Sikarwar (3 years ago)
The location of the .wav file would not be correct. Like I am and using a .wav file named BrickFall.wav and it is in a folder named Sounds inside Resources folder, so I need to write"PlaySound(TEXT("Resources\\Sounds\\BricksFall.wav"), NULL, SND_SYNC); "
Briana Milton (3 years ago)
+Sky Mage Same here.
AZZAMNO1 (3 years ago)
im trollin my collegue with this xD
Ritika Jain (3 years ago)
Thanks for this video,but I want to paly this audio file in linux system.What should I do? Thanx in advance
Tall Guy Productions (3 years ago)
Almost perfect; To get this to work you have to specify the relative (to the project) file path: PlaySound(TEXT("SoundFolder/Explosions/explosion1.wav"), NULL, SND_ASYNC);
RealEyes (3 years ago)
i use eclipse (mars version) ; can you tell me where should i write that in my IDE ?
RealEyes (3 years ago)
+Phloat Wireless i was refering to that library file shit !
Phloat Wireless (3 years ago)
+pascalirinel in your int main() {}
Technology Music (3 years ago)
Thanks. Got the "JOHN CENA" mp3 to play on my buddies computer after downloaded on Skype.
Neco Will (3 years ago)
Thank you got it functioning fine.
Wade Grimm (3 years ago)
I cant see any of the text cus its so dinky. Maybe try zooming in on edits.
Unite Tokyo 2018 (3 years ago)
Nice!!
Matipa Modisane (3 years ago)
Did everythimg right and program runs but instead of play the intended music, it plays a beep sound, please help. Im currently using Microsoft Visual Studio 2012
ANTREA IOANNOU (3 years ago)
+Mamod One hey I know it's been a long time since the last time you posted that but I have to make it work until Monday and I have the same problem.. Can you help me out here? Thanks
PoisonedHive (3 years ago)
Thank you so much! This was really helpful and really informative. It really saved me on this project I am currently working on. After looking around I found an alternative by using threads: #include <iostream> #include <thread> void play_music() {   PlaySound("sleep_away.wav", NULL, SND_FILENAME|SND_LOOP); } int main(int argc, char* argv[]) {   std::thread t(play_music);   //other code   t.join();     return 0; }
mrensayne (4 years ago)
Getting Problems when I try to make a released .exe out of the code you showed.
Agus Roth (4 years ago)
It has no errors warnings neither. It compiles and executes, but it does not play the sound.
Agus Roth (4 years ago)
Thx
Adde r2 (4 years ago)
Okay then good luck in future programming. ;)
Agus Roth (4 years ago)
Dont worry I made it work. I dont remember how I did. Sorry if I wrote bad. I'm studying english.
Adde r2 (4 years ago)
Did you make sure you put the audio as a wav, and in the folder with the cpp file?
It doesn't look it but, the .wav file, is that a file path? Or do I need to put the audio files I want to use in a specific folder cor compilation?
Zorah Ahmad (4 years ago)
God bless you woman :D . Ive been stuck at it for months 
Gareth Squires (4 years ago)
This guide helped a lot thanks but I needed it to loop and be in the background. I did a little more digging and found you can make the music loop and play in the background by using code like this: PlaySound("christmas.wav", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC); Hope this helped anyone who was in the same situation as me!
Mark GM (1 month ago)
Thank you!!
vidaoTime (7 months ago)
fucking liar it doesnt work at all
Maker ofStartup (8 months ago)
Thank you Gareth Squires, and God this is no f..ing stackoverflow, I dont need reputation to up-vote U !!!
Roscovanul2 (2 years ago)
+Gareth Squires I think I'm in love with you ! Thank you :D
Julen Salazar Ponce (3 years ago)
+Gareth Squires Thanks A LOT!!!!!
willy sylla (4 years ago)
very helpful. thanks
Jasaf3 (4 years ago)
Well thx worked but if i want to combine sounds?
Sara Mutahar (10 months ago)
Piotr Kwiatkowski could you please give the simplest way to play multiple sounds ?
Piotr Kwiatkowski (1 year ago)
Unfortunately, you can't, it's very simple library and doesn't support playing more than one sound at a time
Joe Lavoie (4 years ago)
thanks so much worked great
Manhatten ! (4 years ago)
i did everything :D included mmsystem and did pragma load lib winmm but it says PlaySound identifier error -_- almost messed up
Tseno Tsenov (4 years ago)
Can you play mp3 ?
Richard : (4 years ago)
Waveform Audio File(WAV) Format is a Microsoft audio format. This function is from the windows libary. So the answer is no.
Alex Evangelou (4 years ago)
Is there any way to execute the rest of the code while the sound is playing?
elmanuki (4 years ago)
good vid but horrible resolution
Richard J.Rustles Hyman (7 months ago)
Quite the understatement lel, you literally can't see jack on the screen. ....... That said, this video was actually still pretty helpful......
Jafet ChaB (4 years ago)
Thanks a lot!
Megha Nayak (4 years ago)
Very useful, thanks a lot!!!!!!!!
Na Boo (4 years ago)
I love you
modydog2 (4 years ago)
very useful many thnx :)
Charly Botello (5 years ago)
+Joachim Lundberg I have the same problem. Hope somebody can help us :S
tom lukeywood. (5 years ago)
THANK YOU !!!! :D!!!
armagidon eddison (5 years ago)
Thank You : )
Joachim Lundberg (5 years ago)
I want to put sound in a game and when i add it like this nothing else works...? should i put anything else instead of NULL ?
Mr Blade (5 years ago)
This is the first tutorial that has worked for me with PlaySound()! Thank you!
ninoslav (5 years ago)
add additional dependencies !!
Just Aron (5 years ago)
error LNK2019: unresolved external symbol [email protected] referenced in function _main
Kazakouramariou (5 years ago)
change to L"bee.wav" and be done
Gor Matevosyan (5 years ago)
error C2664: 'PlaySoundW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
Kevincentius (5 years ago)
This video's very useful for me XD ! Can it only play *.wav? I tried mid and it didn't work, ehehehe... I'm wondering which other file extensions can be played, if there's any.
Chayan Gulati (5 years ago)
Similar to this there is a sound ( ) which is used to get beeps of different frequencies. Now the sound comes out of the internal speakers , I want the same output via external speakers Can you help please....
MrBojac111 (6 years ago)
Thank you so much, this is the first tutorial that wasn't using windows form app that actually worked.
smarch-rashk suarez (6 years ago)
Me parece sensacional este vídeo profgustin gracias por contribuir a este mundo de jóvenes latinos hambrientos por programar............gracias por trasmitirnos sus conocimientos...................rashk

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.