Looping gapless audio effects on Android

I want a short audio clip (white noise) to play and loop. On Windows (desktop) it works fine, however on Android I notice a gap each time the audio clip ends/starts.

Is this a known issue, and are there any workarounds?

Thank you

Which one do you use to play sound, CocosDenshion::SimpleAudioEngine or experimental::AudioEngine?

@hzlov I’m using experimental::AudioEngine and v3.4.

Hi @mtartist.

I have similar problem here: Background music loop on Android

Did you find some workaround?

Thanks.

The experimental::AudioEngine definitely has an issue on Android with looping smoothly.
I tested with a 7 second long 44.1KHz 8 bit sample mono wave file on Android 5.0 and 5.1.1. With MP3 or WAV, there is a gap in the restarting of the loop.

I’m using android-ndk-r10d.

When using CocosDenshion::SimpleAudioEngine on Android API 21 and 22 with a looping wav file, I get smooth looping. So CocosDenshion::SimpleAudioEngine seems to work good, but we shouldn’t use SimpleAudioEngine on API 21 and the only way you can is if you change the source code in Cocos2d-x becaue…

In Cocos2d-x 3.7rc0, the SimpleAudioEngine on Android no longer exists if you are running an a API 21 device. Instead you get the experimental AudioEngine.
There was a critical bug that resulted in this change. But I cannot reproduce the bug on 5.0 or 5.1.1. But the bug does say you need 5.0.1. I will test on 5.0.1 soon and add a reply to this thread.
https://github.com/cocos2d/cocos2d-x/pull/12098
https://github.com/cocos2d/cocos2d-x/issues/9911

If I change _implementBaseOnAudioEngine = true to _implementBaseOnAudioEngine = false in the following code located at cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp then the code uses SimpleAudioEngine even on Android 5.0 API 21.

    auto sdk_ver = atoi(sdk_ver_str);
    __android_log_print(ANDROID_LOG_DEBUG, "cocos2d", "android build version:%d", sdk_ver);
    if (sdk_ver == 21)
    {
        _implementBaseOnAudioEngine = false;
    }

When I use an mp3 file with SimpleAudioEngine on Android, I get a popping sound on the restart of the loop. On iOS I get a gap in play. But I am ok with that. Using wav files for loops works good.

If I use the experimental audio engine on either API 21 or API 22, then the loop has a noticeable gap regardless of mp3 of wav.

I made the audio file with Adobe Audition and it is a very smooth loop on Mac, iOS, or Android with the SimpleAudioEngine, so definitely not an issue with the audio file.

@lfmn, my solution was to use fmod for all audio processing duties.

Fmod looks like an awesome suite of audio tools.

But at $500/platform you need a decent budget for audio to buy an FMod license, unless you only make one game per year.

We should be able to bring together an open source solution to cover Mac, iOS, Win32, WinRT, Android and Linux that can do basic per effect volume, play rate / pitch, and work as expected.

I have contributed some to try to make the Cocos2d-x audio situation better, but there is more work to be done.

I tested using CocosDenshion::SimpleAudioEngine on a Nexus 7 running 5.0.1 (razor-lrx22c) and do not see any lag in playing a sound effect file.

But looks like others were complaining that they did experience it.
https://github.com/cocos2d/cocos2d-x/issues/9911

Could you provide an audio file for reproduced this issue?

The LoopFiles.zip contains 7 audio loop files of the same audio track, but in different audio formats. I just created these files. Feel free to do anything you want with them.

For gapless smooth audio playback, the following four files can be played on SimpleAudioEngine Android with a good result.
But on AudioEngine Android, there is a significant gap when the loop replays.

drill-44.1KHz-PCM-8bit-mono.wav
drill-44.1KHz-PCM-8bit-stereo.wav
drill-44.1KHz-PCM-16bit-mono.wav
drill-44.1KHz-PCM-16bit-stereo.wav

The next two mp3’s have a small gap on SimpleAudioEngine Android. So mp3 files do not loop good in SimpleAudioEngine or AudioEngine on iOS or Android.
drill-44.1KHz-128kbps-MP3-mono.mp3
drill-44.1KHz-128kbps-MP3-stereo.mp3

This file is not playable on SimpleAudioEngine or AudioEngine on iOS or Android. Only add it here to make a point that we should share with users which formats are supported and which are not.
drill-48KHz-PCM-32bit-stereo.wav

LoopFiles.zip (1009.1 KB)

I can’t find any solution for this issue.
We may went to consider decode audio by a third-party library and streaming with openSL ES,this way we can control audio loop.

I could not find a fix either.

Are you suggesting that we remove the loop feature from AudioEngine?
Or are you saying that we need to find a new open source solution to use in AudioEngine Android?

I am curious about the issue: https://github.com/cocos2d/cocos2d-x/issues/9911

I tested CocosDenshion::SimpleAudioEngine on a Nexus 7 2013 running 5.0.1 (razor-lrx22c) and do not see any lag in playing a sound effect file.

I mean we need consider find a new open source solution(codec library) to use with openSL ES on Android.
issue9911 This problems arise after played multiple audio files(more then six)

Thanks @mtartist and @Heyalda.

I’m using preloadBackgroundMusic, and CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(sound,true).

I didn’t know that different formats of same wav file could have different behavior on Android.

I tested all your wav files using CocosDenshion::SimpleAudioEngine on a Moto X 2014 running 5.1 and I got a gap in playing background music. The music gap is acceptable to me. My problem is the performance of game (there is a little game pause). I’m looking for a workaround to solve this problem.

I’m using ndk-r9d, cocos2d-x 3.4, android:targetSdkVersion=“19”.

Thanks.

@Ifmn,
How many sound files are you playing simultaneously or have you played in total after launching your game when you see the pause issue you associate with the sound file? Since you are using 3.4, maybe you are experiencing this issue https://github.com/cocos2d/cocos2d-x/issues/9911.

Can you set up a simple test project for the gap you are experiencing? On a Nexus 7 and Nexus 9 running 5.0, 5.0.1 and 5.1.1 I did not see a gap when looping the sound files I posted. But I did not try playing multiple files simultaneously or in sequence.

@Heyalda,

My sequence: one playEffect on splash scene, one playEffect and one playBackgroundMusic(sound,loopTrue) on initial scene (in sequence, not simultaneously) then I can see the music gap when loop restarts. During the game I’m playing one background music (loopTrue) and one playEffect for every user’s touch. Sometimes I’m playing four sound effects simultaneously. Maybe my problem is related with this 9911 issue. I’ll do more tests and I let you know if I could reproduce this gap on a test project.

Thanks again for your help.

Hi @Heyalda,

I’m testing on a Nexus 5 running Android 5.1.1. I see the gap both SimpleAudioEngine (playBackground) and AudioEngine (play2d). I’ve got problems related with 9911 issue using playEffect (SimpleAudioEngine) on Android 5.1.1, but it’s ok with Android 2.3.4, 2.3.6 and 4.1.

When I play background music I see the frame rate dropped after a few seconds (Android 5.1.1 with both AudioEngine and SimpleAudioEngine), but if play some sound effects with play2d during the game (touching the screen, for example) I do not see this frame rate problem.

If I play a muted sound in loop during the game (http://www.thiagorosa.com.br/how-to/improve-soundpool-performance) the frame rate is ok with SimpleAudioEngine (playEffect loop=true) on Android 5.1.1.

Now, I’m trying to use:
If Android version <= 4.4 (KitKat):

  • SimpleAudioEngine for background music (With lag on loop).
  • SimpleAudioEngine for effects (playEffect).

If Android version >= 5.0 (Lollipop):

  • SimpleAudioEngine for backgroundMusic (With lag on loop).
  • AudioEngine for effects (play2d).

I’m using ndk-r9d, cocos2d-x 3.4, android:targetSdkVersion=“19”.

Thanks again.

@ifmn, it is important to note that due to issue 9911, a change to the audio in Cocos2d-x was made.

This is the change: https://github.com/cocos2d/cocos2d-x/pull/12098
These are the files that were changed: https://github.com/cocos2d/cocos2d-x/pull/12098/files

So if you are running your app on an Android device running API 21 (5.0.x), then the SimpleAudioEngine will dynamically switch its implementation to use the new AudioEngine. On API 22 (5.1.x) SimpleAudioEngine will not change to use the new AudioEngine automatically.

API 21 == 5.0.x
API 22 == 5.1.x

You should not be seeing issue 9911 on a 5.1.1 device. If you are, you should comment on the 9911 issue in github with supporting information to help reproduce it, since the fix for 9911 only covers API 21 (5.0.x).

By the way, @WenHai said 9911 can be reproduced by playing multiple audio files (more than 6).

The testing that I did was on a version before the 3.7 changes to force using AudioEngine on API 21.

With that said, I just realized I need to test with some longer duration sound files. So far I have only been testing with sound files with a max duration of about 10 seconds.

1 Like

This issue was fixed at the latest code of v3 branch. And it will be in the cocos2d-x 3.13 (probably released in 2016.8.22).

If you guys are able to read Chinese, there’s a issue at https://github.com/cocos2d/cocos2d-x/issues/16144.
And the issue reporter said that the problem was fixed at the latest code in v3 branch. :slight_smile:

I’m using cocos2d-x 3.13 and problem persists.