Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use PlaySound() in C++/OpenGL to play sound in background

I am trying to play a wav file in the background of a game built in c++ with opengl. I am using the following line to play the wav file:

 PlaySound("starwars.wav", NULL, SND_FILENAME|SND_LOOP);

The problem is when the music starts the animation stops. I tried starting the music at the press of a keyboard button, but when I do that the music starts and the all the animation stops. Is there a way to avoid this? I just want some music to play in the background and PlaySound seemed the simplest way to achieve that, given the fact it requires just a line of code.

like image 959
biggdman Avatar asked Jan 10 '12 14:01

biggdman


2 Answers

You want to pass in

SND_ASYNC

This would make PlaySound return immediately, rather than waiting for the sound to finish playing, which in your case wouldn't as you are looping. IIRC PlaySound only allows one sound to play at any one time so it may be best to look for a sound library, especially if you are making a game.

In conclusion for your sample to work:

PlaySound("starwars.wav", NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);

Please see this

like image 67
Carl Winder Avatar answered Oct 09 '22 10:10

Carl Winder


If you are using the format playSound("*.wav",NULL,SND_SYNC|SND_LOOP) almost you are out of control of the animation of the game which will be frozen with the loop included in the playSound() function which is SND_LOOP but if you change the SND_SYNC with SND_ASYNC it would work exactly as you ordered but do not forget that this works for windows and do not forget to include the WINMM.LIB (window multimedia library) under the project/opengl/visual c++/link

like image 29
yemane Avatar answered Oct 09 '22 11:10

yemane