Game is frozen and lagged on one device by sound effects on Android [Solved]

Hello. I have one problem with my game project on cocos2d-x on Android.
I use Audio Engine for playing sounds effects. Sounds are played normally on most Android devices, but on one device the game is frozen and lagged in an end of playing sound. The FPS usually is decreased to 20-30 from 60 in these cases. Another games (on cocos2d-x from google play) work well on this device.
I tried to use Simple Audio Engine. At the beginning it didn’t help me, but after then i enabled a background music in the game, my game was stopped freezing and lagging very on this device. But with this audio engine my game often is lagged and frozen a bit on ALL Android devices.
Yes, it is strange, i understand.

Platform: Android v4.3
Sounds’ format: mp3
The problem device: Highscreen Boost 2 SE
Engine version: cocos2d-x 3.10
Audio: Audio Engine
The device’s logs:

--------- beginning of /dev/log/main
D/dalvikvm(32054): Late-enabling CheckJNI
D/dalvikvm(32054): Trying to load lib /data/app-lib/org.cocos2dx.Rosunka-1/libcocos2dcpp.so 0x418c3530
D/dalvikvm(32054): Added shared lib /data/app-lib/org.cocos2dx.Rosunka-1/libcocos2dcpp.so 0x418c3530
D/JniHelper(32054): JniHelper::setJavaVM(0x41606100), pthread_self() = 1074733052
D/main (32054): cocos_android_app_init
D/Cocos2dxActivity(32054): model=Boost IIse
D/Cocos2dxActivity(32054): product=D10A_HighScreen
D/Cocos2dxActivity(32054): isEmulator=false
D/Cocos2dxActivity(32054): onResume()
D/libEGL (32054): loaded /vendor/lib/egl/libEGL_adreno.so
D/libEGL (32054): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
D/libEGL (32054): loaded /vendor/lib/egl/libGLESv2_adreno.so
I/Adreno-EGL(32054): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_3.2.1_RB3.04.03.00.154.030_msm8226_JB_3.2.1_RB3__release_AU ()
I/Adreno-EGL(32054): OpenGL ES Shader Compiler Version: 20.00.05
I/Adreno-EGL(32054): Build Date: 01/02/14 Thu
I/Adreno-EGL(32054): Local Branch:
I/Adreno-EGL(32054): Remote Branch: quic/jb_3.2.1_rb3.13
I/Adreno-EGL(32054): Local Patches: NONE
I/Adreno-EGL(32054): Reconstruct Branch: AU_LINUX_ANDROID_JB_3.2.1_RB3.04.03.00.154.030 + c360582 + NOTHING
D/OpenGLRenderer(32054): Enabling debug mode 0
D/cocos2d-x debug info(32054):
D/cocos2d-x debug info(32054): {
D/cocos2d-x debug info(32054): gl.supports_vertex_array_object: true
D/cocos2d-x debug info(32054): gl.supports_BGRA8888: false
D/cocos2d-x debug info(32054): gl.supports_ATITC: true
D/cocos2d-x debug info(32054): gl.supports_S3TC: false
D/cocos2d-x debug info(32054): cocos2d.x.version: cocos2d-x-3.10
D/cocos2d-x debug info(32054): gl.supports_discard_framebuffer: true
D/cocos2d-x debug info(32054): cocos2d.x.compiled_with_profiler: false
D/cocos2d-x debug info(32054): gl.supports_PVRTC: false
D/cocos2d-x debug info(32054): cocos2d.x.build_type: DEBUG
D/cocos2d-x debug info(32054): gl.renderer: Adreno ™ 305
D/cocos2d-x debug info(32054): gl.supports_ETC1: true
D/cocos2d-x debug info(32054): cocos2d.x.compiled_with_gl_state_cache: true
D/cocos2d-x debug info(32054): gl.version: OpenGL ES 3.0 V@53.0 AU@04.03.00.154.030 (CL@)
D/cocos2d-x debug info(32054): gl.supports_NPOT: true
D/cocos2d-x debug info(32054): gl.max_texture_units: 32
D/cocos2d-x debug info(32054): gl.vendor: Qualcomm
D/cocos2d-x debug info(32054): gl.max_texture_size: 4096
D/cocos2d-x debug info(32054): }
D/cocos2d-x debug info(32054):
D/cocos2d-x debug info(32054):
D/CocosPlayClient.cpp(32054): isEnabled = 0
D/CocosPlayClient.cpp(32054): isDemo = 0
D/CocosPlayClient.cpp(32054): isNotifyFileLoadedEnabled = 0
D/cocos2d (32054): android build version:18
D/CCFileUtils-android.cpp(32054): relative path = Animations/flower.gaf
D/Cocos2dxActivity(32054): onWindowFocusChanged() hasFocus=true
D/CCFileUtils-android.cpp(32054): relative path = Animations/insect_ace.gaf

D/CCFileUtils-android.cpp(32054): relative path = Animations/multipurposal death.gaf
D/cocos2d-x debug info(32054): create rendererRecreatedListener for GLProgramState

D/cocos2d-x debug info(32054): create rendererRecreatedListener for GLProgramState

Thanks for the help. Sorry for my English.

For playing sounds i use standart function:

cocos2d::experimental::AudioEngine::play2d(“sound.mp3”);

So it runs at 60fps as long if you comment out the play2d calls?
So both SimpleAudioEngine playEffect and experimental::AudioEngine::play2d cause lag when playing sound?
Have you tried using .wav versions of your sounds, just to see if the issue is with mp3 playback or audio engine in general?
Same thing, but using .ogg?
How many are you playing at once or in short period?

I and many others have the same problems. Solution: Don’t use Cocos’ audio, use this: http://www.crickettechnology.com/

Here’s a topic by me discussing some of the integration:

Thanks for the info. There’s also SuperPowered ( http://superpowered.com/ ) and FMod ( ) if you’re looking into external audio engines.

Although this one is free AND works on Windows as well.

Great, maybe cocos2d-x can get a similar backend “support” like they have with fmod on linux integrated so users can just enable cricket’s audio by downloading the sdk, “plugging” it into the correct folder (or whatever) and then setting a #define correctly.

I’m just letting people know their options.

  • Cocos2d-x AudioEngine: we’ve had no lag issues or audio problems since we started using this.
  • Superpowered: Android/iOS only, but most cocos2d-x games only care about these two platforms.
  • FMod is free up to $100K revenue, but regardless most people won’t use it (unless they want the tools)
  • SDL audio is another option.
    Cocos2d-x Sound engine alternative

Everything looks to use OpenSL ES on Android, so any issue that cocos2d-x has with devices should be solvable, or they’re issues that the other engines should have too - such as a specific device’s erroneous implementation of the OpenSL driver.

Thanks for the help.

  1. Yes. 58-60 fps if sounds don’t work
  2. Yes. Two. One - more lags, another - lass lags.
    1. I use mp3, ogg, wav. All don’t help me
  3. Near 1-2. Max - 3.

So, I wrote a crutch in my game which starts playing all sounds at the beginning of the game and pauses them at the same time, and after, when i want, it plays a needed sound and doesn’t remove it from the cache, it only stops this sound and i can play this sound again. (I use the new Audio Engine);
It helped me. Now the device isn’t being frozen and isn’t being lagged because it was frozen at the end of playing some sound. But it’s the crutch and it isn’t stable.
How can i fix this without the crutch?

@Mishok43 well if you have a little time and are willing check out integrating http://www.crickettechnology.com/ as mentioned above. Prob will save time and headaches in the long run.

You can preload audio. You shouldn’t need to play and then pause, just preload. Also has a callback if you want to play it once loaded, or have some other code run.

experimental::AudioEngine::preload(filepath);

There’s an AudioProfile class that you can pass into play2d that may help with playing too many sounds at once, or playing too many over a period. You can pass in a custom one, or you can getDefaultProfile() and then update it yourself. I don’t believe this is used by default. YMMV and not sure this will fix anything or give you your desired audio capabilities.

cocos2d::experimental::AudioProfile profile;
profile.name = "normal_sounds";
profile.maxInstances = 3;
profile.minDelay = 0.2;
experimental::AudioEngine::play2d(filepath, loop, vol, &profile);

Edit: fixed to show using new profile instead of default

@stevetranby But Audio Engine doesn’t support preload on Android. I saw the source of cocos2d-x 3.11 on Github.

Ah, that’s right. Sorry, I haven’t looked through the audio internals in a while. We have our own AudioManager wrapper around simple and experimental audio engines. We have a preloader and other things related to keeping concurrent playback in check.

Again, if you’re not having issues any more with the play/stop, then don’t worry about it. To fix without the “crutch” you’ll have to wait for the dev team to implement an audio buffer solution on android, which may take forever.

If you’re having issues then you’ll need to use cricket some other 3rd party solution.

My guess is you have no desire to implement anything yourself, but if you do you can look at the google samples.

AudioEcho (google’s low latency audio use case sample)

Better yet, looks like they’ve done a little work toward this issue as well as allowing for preloading.

Thanks, I’m going to try it

Thanks, it helped me. :slight_smile:

1 Like

Can I ask the behavior of your game before and after dumganhar’s patch?
I’m testing this too, but I do not se visible performance improvements!

Thanks

Hello, @drakon99
Well, if we are comparing SimpleAudioEngine and Audio Engine with this patch on android, now my game is being lagged and frozen less than before. If we are comparing Audio Engine without this patch and with on Android too, perfomance wasn’t changed on the most of devices, but this patch increased perfomance on some my devices, which were being lagged and frozen without this patch.

Hi @Mishok43

If I understood well: with AudioEngine things does not changed much, while with SimpleAudioEngine performance increased?
For me the big performance penalty is due to sound preloading not working properly!

Is there any solution for JS Bindings? I downloaded it but my problem persists, but my game lags not because of the sound effects, only when I play the background music. I don’t know what to do.