Search code examples
c++asynchronousaudioplaysound

How to play sound at the same time as the program?


I want to play sound while the program is running in c++. I am using the code:

 void fighterplanesound() 
{PlaySound(TEXT("bombdrop.wav"), NULL, SND_FILENAME|SND_ASYNC);}

I thought the SND_ASYNC flag would fix this problem but it did not. What is happening is the program will pause or control will not move on until the sound is done playing. How can I play the sound while the program continues to run. To clarify, the program is a Windows console program that operates in an MS-DOS window.


Solution

  • Ok. What I figured out was that you can't include the PlaySound method in the function because even though the ASYNC flag was there, control had to fully execute the PlaySound method before returning, so don't put it in the function unless there is more code in the function(in which case the sound will play while that code is executing) or in other words, control will not return from the function until PlaySound is finished executing and because there is no other code in the function to execute, control waits until PlaySound method is finished and then moves on.