Play a sound in loop without interruption

Hello,

Do you know how play a sound in loop without interruption ?

This is my code :

auto audio = SimpleAudioEngine::getInstance();
audio->playEffect(“meche.mp3”, true, 1.0f, 1.0f, 1.0f);

There is a blank of 1 seconde after the sound finish and begin again.

In advance thanks,

Not with mp3s. The way that mp3’s are structured means that there is always a lead in and lead out. So you have to use .wav for looping sounds - or perhaps ogg. While it would be theoretically possible to create a looping frame for mp3s that would seemlessly loop back to the second frame, this has not been done and the result would not be a mp3 file so… :confused:

1 Like

Thanks for your answer, i try with wav and ogg format but the problem is the same.

I don’t suppose you’ve check the sound in an audio editor to check that the sound starts exactly at 0:00 and finishes at the same time as the sound ends?

1 Like

Yes i check that point, the sound start at 0.00 and there is no blank in the end

Hi @versailles,

I have same problem. You can check Looping gapless audio effects on Android and Background music loop on Android maybe can help.

For now, I see the gap on Android >= 5.0 and I’m using it anyway.

1 Like

Ok, thanks a lot for yours answers,

I found some kind of solution with the new AudioEngine.

To avoid the gap, I make sure to “ready” the sound 2 seconds before I need to play it:
musicId = AudioEngine::play2d(nextLoop());
AudioEngine::pause(musicId);

Then, when I need to play it:
AudioEngine::resume(musicId);

I actually use 2 schedulers to achieve that.
Director::getInstance()->getScheduler()->schedule([](float dt) { musicId = AudioEngine::play2d(nextLoop()); AudioEngine::pause(musicId); }, Director::getInstance(), 16.0f, CC_REPEAT_FOREVER, 0.01f, false, "readyMusic");
Director::getInstance()->getScheduler()->schedule([](float dt) { AudioEngine::resume(musicId); }, Director::getInstance(), 16.0f, CC_REPEAT_FOREVER, 2.01f, false, "playMusic");

You can easily guess that I have music pieces of 16 seconds each. The nextLoop() function returns the next piece to play, and musicId is a global variable.

I hope it helps.