سؤال

PlaySound works perfectly fine if i need a single beep. The following illustrates my code snippet:

PlaySound(TEXT("C:\\Test1.wav"), NULL, SND_ASYNC);

My question is, how to use this function twice or more than twice, since it plays only once regardless of the amount of repetition I put together. I have also tried this:

PlaySound(TEXT("C:\\Test1.wav"), NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);

which plays the beep in a continuous loop. How to play this twice or thrice etc.?

Using a loop doesn't help either.

هل كانت مفيدة؟

المحلول 2

The problem is the SND_ASYNC flag. It says that you want this call to return immediately and play the sound in background.

If you do this in a loop, since it will return with the sound still playing, it will overlap with the next call and do nothing, because IIRC, an application can only do one call to PlaySound at the same time.

The solution is to replace SND_ASYNC with SND_SYNC (or remove it, since it is the default). If you need the call to be asynchronous, you can create a thread and equeue the sounds you want to play. The thread will play synchronously, but your other threads won't notice.

نصائح أخرى

You have two options with that API. Either play synchronous, or asynchronous.

If you play synchronous, then your thread will block until the sound has finished playing. You can put it in a loop and call it as many times as you like, but then your thread will block whilst the sound is being played.

If you play asynchronous, then your thread will not block but you don't have any reliable way of controlling the number of repetitions.

I see two solutions for you: either create a new sound from the original, with the correct number of repeats added and play that once, or create a background thread to play the sound the specified number of times - in this background thread you can safely use the synchronous play method as described above.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top