PreloadEffect very slow loading

Hi,
I’m using cocos2d-x 3.13.1 with latest ndk, running latest android 7.1.1 on my nexus 6p and I’ve encountered a problem with loading sounds with preloadEffect. They’re loading very slow, especially on this high-end device. There are 92 sounds and I’ve measured it’s taking about 25 seconds to load them. They’re just 5-30kb each file. OGG format. On the other hand I have very old, slow Lenovo tablet with android 4.2 and they’re loading for only few seconds.
I’ve found some issues about it:

And I’ve found this post:
http://blog.cocos2d-x.org/2015/04/performance-issue-caused-by-simpleaudioengine-on-android-5-0/

But I can still see SoundPool in latest version of cocos2d-x, even on github:

Some said problem is solved:

Switching from OGG to WAV speeded up loading time to about 7 seconds.

But that’s not a perfect solution. OGG files are 2.6MB, while WAV are 21.2MB.

OGG is loseless packaged format, maybe it needs enough resource.

But why is it so slow? If it’s still SoundPool bug, which was first introduced in android 5.0, and we’re on android 7.1.1 now it should be replaced by different sound engine long time ago.

Anyone, help?

Anyone? I think it’s a critical bug. Ogg’s are just unusable if you need many of them at once. They’re loading way too long.

Looks like it’s the same issue I’m having with Ogg files on Android 7.1.1 (Nexus 6P as well):

New AudioEngine uses OpenSL ES, not SimpleAuidoEngine.

AudioEngine is still experimental, I tried it, but it’s even worse:

01-18 14:27:42.942 18845-19174/pl.rosmedia.music E/AudioPlayerProvider: FileInfo (0xc3434178), preloadEffect (assets/sounds_ogg/xylo6.ogg) failed
01-18 14:27:42.942 18845-19174/pl.rosmedia.music E/cocos2d-x debug info: Oops, player is null …

App freezes for few seconds and then sound plays.

Ofc file exists, it plays with SimpleAudioEngine (but as I said loading time is very long):

01-18 14:32:42.974 24960-25656/pl.rosmedia.music W/SoundPool: sample 49 not READY

It’s SoundPool, no way it’s not :slight_smile:

We are trying to decoding audio files by ourself rather than OpenSLES API.
Since on android, using OpenSLES API to decode ogg, mp3 to pcm buffer will cause bundle of system issues that we can’t fix.

I have submitted a pull request for this (https://github.com/cocos2d/cocos2d-x/pull/17233)
Could you have a try and give me feedback.

SimpleAudioEngine is really slow on some devices, and because it also uses system public SoundPool API, so we couldn’t fix it too.

I tried to use it, but I’m getting an error: Cannot find module with tag ‘tremolo/prebuilt-mk’.
though it exists in external directory.

I don’t know why it didn’t work for you. But you could see that my pull request passed Travis-CI: https://travis-ci.org/cocos2d/cocos2d-x/jobs/193298160

Did you run cpp-tests for testing or run in your project?

Yes, cpp-test does build indeed. Thing is I switched to precompiled libraries and created them using cocos gen-libs. It builded without any errors and I can see libvorbisidec.a and libpvmp3dec.a in prebuilt/android directory. But using these instead of unchanged cocos2d-x libs causes an error, which I’ve posted above. Here’s my test project: PreloadBug.zip (5.6 MB)
You have to build static libraries first:
cocos gen-libs -p android --app-abi armeabi-v7a
Then change paths in build.gradle and settings.gradle to your cocos2d-x root.
I guess it’s some tiny mistake, but I don’t know why it’s telling that tremolo is missing.

I tried to build your project.

$ export COCOS_X_ROOT=/Users/james/Project/cocos2d-x
$ cocos run -p android

Building mode: debug
Using Android Studio project : /Users/james/Downloads/PreloadBug/proj.android-studio
running: '/Users/james/Software/android/android-sdk/tools/android update project -t android-10 -p /Users/james/Downloads/PreloadBug/proj.android-studio/app'

Updated project.properties
Updated local.properties
Updated file /Users/james/Downloads/PreloadBug/proj.android-studio/app/proguard-project.txt
Building native...
NDK build mode: debug
running: '/Users/james/bin/android-ndk/ndk-build -C /Users/james/Downloads/PreloadBug/proj.android-studio/app -j8 NDK_MODULE_PATH=/Users/james/Project/cocos2d-x/cocos2d-x-3.14:/Users/james/Project/cocos2d-x/cocos2d-x-3.14/cocos:/Users/james/Project/cocos2d-x/cocos2d-x-3.14/external:/Users/james/Project/cocos2d-x/cocos2d-x-3.14/cocos/prebuilt-mk NDK_TOOLCHAIN_VERSION=4.9 NDK_DEBUG=1'

Android NDK: WARNING: Ignoring unknown import directory: /Users/james/Project/cocos2d-x/cocos2d-x-3.14
Android NDK: WARNING: Ignoring unknown import directory: /Users/james/Project/cocos2d-x/cocos2d-x-3.14/cocos
Android NDK: WARNING: Ignoring unknown import directory: /Users/james/Project/cocos2d-x/cocos2d-x-3.14/external
Android NDK: WARNING: Ignoring unknown import directory: /Users/james/Project/cocos2d-x/cocos2d-x-3.14/cocos/prebuilt-mk
Android NDK: jni/Android.mk: Cannot find module with tag './prebuilt-mk' in import path
Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?
Android NDK: The following directories were searched:
Android NDK:
make: Entering directory `/Users/james/Downloads/PreloadBug/proj.android-studio/app'
jni/Android.mk:26: *** Android NDK: Aborting.    .  Stop.
make: Leaving directory `/Users/james/Downloads/PreloadBug/proj.android-studio/app'

Why it went to find /Users/james/Project/cocos2d-x/cocos2d-x-3.14 since I exported /Users/james/Project/cocos2d-x

You have open it with android studio. It’s the easiest way. cocos console is overwriting some paths.

I guess you were using prebuilt libraries.
It’s the bug in cocos2d-console of generating prebuilt libraries.
Some thing you have to modify:

  • cocos2d-x/tools/cocos2d-console/plugins/plugin_generate/configs/gen_libs_config.json:

Adds

    "android_mks": [
        "cocos/Android.mk",
        "cocos/audio/android/Android.mk",
        "cocos/scripting/js-bindings/proj.android/Android.mk",
        "cocos/scripting/lua-bindings/proj.android/Android.mk",
        "cocos/storage/local-storage/Android.mk",
        "external/bullet/Android.mk",
        "external/recast/Android.mk",
        "external/flatbuffers/Android.mk",
        "external/Box2D/Android.mk",
        "external/tremolo/Android.mk", // Adds this line 
        "external/pvmp3dec/Android.mk", // Adds this line
        "cocos/editor-support/cocosbuilder/Android.mk",
        "extensions/Android.mk",
        "cocos/3d/Android.mk",
        "cocos/ui/Android.mk",
        "cocos/editor-support/cocostudio/Android.mk",
        "cocos/network/Android.mk",
        "cocos/platform/android/Android.mk",
        "cocos/editor-support/spine/Android.mk",
        "tools/simulator/libsimulator/proj.android/Android.mk"
    ],
  • Modify external/tremolo/Android.mk

LOCAL_SRC_FILES = \ to LOCAL_SRC_FILES := \

LOCAL_MODULE := libvorbisidec to LOCAL_MODULE := vorbisidec

  • Modify external/pvmp3dec/Android.mk

LOCAL_MODULE := libpvmp3dec to LOCAL_MODULE := pvmp3dec

Re-generate prebuilt libraries and test it again.

Thanks, now it works.
Yes, I’m using precompiled libs.
Does it work with AudioEngine only?
I tried first on SimpleAudioEngine and nothing changed. But on AudioEngine it’s blazing fast and playing!

Here are some results:

Nexus 6P:
10 oggs: 0.2s
10 waves: 0.17s

Lenovo A1000L-F:
10 oggs: 0.7s
10 waves: 0.025s (wtf)

Samsung Galaxy A3 (2016):
10 oggs: 0.46s
10 waves: 5.8s (strange!)

My updated test project:
PreloadBug.zip (5.6 MB)

Yes, wav decoding is still using the System OPENSLES API which will be very slow on some specific devices.
I will add wav support soon.