0

My program is playing a sound PlaySound.

The program works fine and I can hear the sound, but when the song end, there is a delay for like 1 second, and then the song play again.

I asked Google, and he gave me this question - PlaySound() Delay

The guy who answerd , said that instead SND_SYNC we need to use SND_ASYNC, I listened to him and did it, but I can't hear anything.

Do you have any suggestions ?

Btw, this is the song I'm currently using for this project - Nyan Cat

I want that this song will be start again immediately, for the user to not hear that there is a Delay.

Final Code:

#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")

int main()
{
    std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
    while (true) {
        PlaySound(pathtosound.c_str(), 0, SND_SYNC);
    }

    return 0;
}
  • 2
    Did you check `nyan.wav` itself to make sure there is not 1 second of silence at the end of its audio? BTW, why are you calling `PlaySound()` in a manual loop instead of calling it once with the `SND_LOOP` flag? "*The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. If this flag is set, you must also set the SND_ASYNC flag.*" – Remy Lebeau Dec 01 '19 at 19:06
  • @RemyLebeau I have edited the song to 4 sec long, I make it to start from the center, but I still hear 1 second of silence when the song end. –  Dec 01 '19 at 19:17
  • @RemyLebeau `SND_LOOP` is not playing the sound over and over, only 1 time, even when I call it with `SND_ASYNC` the sound is not even playing. –  Dec 01 '19 at 19:19
  • @Mr.Strings please [edit] your question to show your updated code – Remy Lebeau Dec 01 '19 at 20:24

1 Answers1

1

The SND_LOOP flag is described as follows in Microsoft Docs:

The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. If this flag is set, you must also set the SND_ASYNC flag.

Pay attention to the last sentence, hence the following code will probably work better:

#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")

int main()
{
    std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
    PlaySound(pathtosound.c_str(), 0, SND_ASYNC | SND_LOOP);
    while (true) {
        // Stop loop at some point
    }
    PlaySound(NULL, 0, 0);  // Stop sample

    return 0;
}
arghol
  • 745
  • 9
  • 21
  • I have up voted your answer, but I can still hear a delay, I have wrote like you writed, I have edited the video to be length of only 4 seconds. –  Dec 01 '19 at 20:40
  • @Mr.Strings Are you sure that it is not a problem with your file? Have you tried looping the wave file in a audio editor, e.g. Audacity? – arghol Dec 01 '19 at 20:47
  • Hey, can you please [download](https://gofile.io/?c=H4PawB) and you can see that the audio file is at 4 second length and there is no problem with the file, but maybe you will see something wrong. –  Dec 01 '19 at 20:57
  • @Mr.Strings There is actually a short silent part at the beginning of the audio file. You need to edit the file to make it a smooth loop. That is not always so easy. Making a good loop is a kind of art. – arghol Dec 01 '19 at 21:06
  • You are good at it ? Maybe you can help me with this please ? –  Dec 01 '19 at 21:11
  • @Mr.Strings I've [clipped](https://gofile.io/?c=rIIVNm) it for you (was bored) – Jonathan Potter Dec 01 '19 at 22:25
  • @JonathanPotter Thanks, But this is not seem loop, we can still hear the delay, but thanks. –  Dec 02 '19 at 11:36