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

36,114

Solution 1

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

Solution 2

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

Share:
36,114

Related videos on Youtube

biggdman
Author by

biggdman

bigdinrock

Updated on June 14, 2020

Comments

  • biggdman
    biggdman almost 4 years

    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.

  • Carl Winder
    Carl Winder over 12 years
    No problem, don't forget to mark this as the answer :) I also think you should look into a sound API see this SO question stackoverflow.com/questions/3636715/…
  • Osman Gani Khan Masum
    Osman Gani Khan Masum over 5 years
    can I Ask, where to put this line of code? In Main(), or in the animation function?