2
\$\begingroup\$

I am new to OpenGL. I am making a game similar to this, shown in image below. The problem is when I detect collision, i.e cannon ball and ship collide, I play a sound using

PlaySound(TEXT("explode.wav"), NULL, SND_FILENAME);

After the sound is played, my background animation and the whole scene is stuck for a second and then it resumes. Any fix? Please help. It is my project. enter image description here

\$\endgroup\$
6
\$\begingroup\$

Looking at the documentation of PlaySound() on MSDN it states:

The SND_ASYNC flag causes PlaySound to return immediately without waiting for the sound to finish playing.

The conclusion from this information, although not explicitly stated there, is that PlaySound() waits for the sound to finish playing, if the SND_ASYNC flag is not specified. Therefore the lag you experience when playing a sound has not to do with OpenGL but with the fact that PlaySound() just waits for the sound to finish before it returns, thus blocking the calling thread.

So the proper code for non-blocking (i.e. asynchronous) sound should be:

PlaySound(TEXT("explode.wav"), NULL, SND_FILENAME | SND_ASYNC);

If you want to stop an asynchronous sound before it is finished, use

PlaySound(NULL, 0, 0);
\$\endgroup\$
2
  • \$\begingroup\$ in my init() there is PlaySound(TEXT("ocean.wav"), NULL, SND_ASYNC | SND_LOOP); , so when i play explode sound i.e PlaySound(TEXT("explode.wav"), NULL, SND_FILENAME | SND_ASYNC); , the looping sound is finished, is there any way to keep it going ? \$\endgroup\$ Dec 25 '16 at 14:27
  • 2
    \$\begingroup\$ There surely is a way, but probably not with PlaySound only, because PlaySound seems to be a rather primitive function that is not very helpful, if you want to manage several sounds playing simultaneously. Maybe you should look for other means to manage the sound. Perhaps OpenAL is an option for you? \$\endgroup\$
    – Striezel
    Dec 25 '16 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .