[Android] Audio Decoding Issues Discussion

I did it like this:

cocos gen-libs -c -p android --app-abi armeabi-v7a

-c should clean first, right?

I did make changes too.

Log:

01-24 10:47:35.411 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background1.ogg done loading. Result: success
01-24 10:47:35.493 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background2.ogg done loading. Result: success
01-24 10:47:35.509 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background8.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background5.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background6.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background4.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background9.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background7.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background3.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background10.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: All done!
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: loading took: 2184 ms

Please upload the whole log and don’t make a filter for log.

01-24 10:47:19.463 5314-5314/? I/art: Late-enabling -Xcheck:jni
01-24 10:47:19.630 5314-5314/org.cocos2dx.ProjectName W/ActivityThread: Application org.cocos2dx.ProjectName is waiting for the debugger on port 8100…
01-24 10:47:19.636 5314-5314/org.cocos2dx.ProjectName I/System.out: Sending WAIT chunk
01-24 10:47:26.646 5314-5321/org.cocos2dx.ProjectName I/art: Debugger is active
01-24 10:47:26.689 5314-5314/org.cocos2dx.ProjectName I/System.out: Debugger has connected
01-24 10:47:26.689 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:26.889 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:27.090 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:27.290 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:27.491 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:27.692 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:27.893 5314-5314/org.cocos2dx.ProjectName I/System.out: waiting for debugger to settle…
01-24 10:47:28.094 5314-5314/org.cocos2dx.ProjectName I/System.out: debugger has settled (1376)
01-24 10:47:29.504 5314-5314/org.cocos2dx.ProjectName D/JniHelper: JniHelper::setJavaVM(0xf0fb20c0), pthread_self() = -205470412
01-24 10:47:29.504 5314-5314/org.cocos2dx.ProjectName D/main: cocos_android_app_init
01-24 10:47:29.507 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxHelper: isSupportLowLatency:true
01-24 10:47:29.512 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxHelper: sampleRate: 48000, framesPerBuffer: 192
01-24 10:47:29.512 5314-5314/org.cocos2dx.ProjectName D/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp: nativeSetAudioDeviceInfo: sampleRate: 48000, bufferSizeInFrames: 192
01-24 10:47:29.802 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: model=Nexus 6P
01-24 10:47:29.802 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: product=angler
01-24 10:47:29.802 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: isEmulator=false
01-24 10:47:29.811 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: onResume()
01-24 10:47:29.939 5314-6093/org.cocos2dx.ProjectName I/Adreno: QUALCOMM build : a5b4970, If5818605d9
Build Date : 10/12/16
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch : N24D
Remote Branch :
Remote Branch :
Reconstruct Branch :
01-24 10:47:29.986 5314-6093/org.cocos2dx.ProjectName I/OpenGLRenderer: Initialized EGL, version 1.4
01-24 10:47:29.986 5314-6093/org.cocos2dx.ProjectName D/OpenGLRenderer: Swap behavior 1
01-24 10:47:30.731 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: onWindowFocusChanged() hasFocus=true
01-24 10:47:33.358 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: Started preloading!
01-24 10:47:33.368 5314-6092/org.cocos2dx.ProjectName I/AudioPlayerProvider: deviceSampleRate: 48000, bufferSizeInFrames: 192
01-24 10:47:33.369 5314-6092/org.cocos2dx.ProjectName D/AudioPlayerProvider: Android API level: 25
01-24 10:47:33.369 5314-6092/org.cocos2dx.ProjectName I/libOpenSLES: Emulating old channel mask behavior (ignoring positional mask 0x3, using default mask 0x3 based on channel count of 2)
01-24 10:47:33.379 5314-6092/org.cocos2dx.ProjectName D/AudioTrack: Client defaulted notificationFrames to 192 for frameCount 384
01-24 10:47:35.384 5314-6092/org.cocos2dx.ProjectName D/ThreadPool: stretch pool from 1 to 3, waste 0.001924 seconds
01-24 10:47:35.388 5314-6092/org.cocos2dx.ProjectName D/ThreadPool: stretch pool from 3 to 5, waste 0.001685 seconds
01-24 10:47:35.405 5314-6092/org.cocos2dx.ProjectName D/ThreadPool: stretch pool from 5 to 7, waste 0.015279 seconds
01-24 10:47:35.409 5314-6092/org.cocos2dx.ProjectName D/ThreadPool: stretch pool from 7 to 8, waste 0.001649 seconds
01-24 10:47:35.411 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background1.ogg done loading. Result: success
01-24 10:47:35.493 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background2.ogg done loading. Result: success
01-24 10:47:35.509 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background8.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background5.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background6.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background4.ogg done loading. Result: success
01-24 10:47:35.526 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background9.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background7.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background3.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: background10.ogg done loading. Result: success
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: All done!
01-24 10:47:35.542 5314-6092/org.cocos2dx.ProjectName D/cocos2d-x debug info: loading took: 2184 ms
01-24 10:47:38.095 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: onWindowFocusChanged() hasFocus=false
01-24 10:47:38.097 5314-5314/org.cocos2dx.ProjectName D/Cocos2dxActivity: onPause()
01-24 10:47:38.572 5314-5321/org.cocos2dx.ProjectName W/art: Suspending all threads took: 248.436ms
01-24 10:47:38.916 5314-5314/org.cocos2dx.ProjectName W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
01-24 10:47:38.959 5314-5314/org.cocos2dx.ProjectName W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-24 10:47:38.963 5314-5314/org.cocos2dx.ProjectName W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
01-24 10:47:38.965 5314-5314/org.cocos2dx.ProjectName W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
01-24 10:47:39.097 5314-5314/org.cocos2dx.ProjectName W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
01-24 10:52:36.536 5314-5321/org.cocos2dx.ProjectName W/art: Suspending all threads took: 8.645ms
01-24 10:53:14.768 5314-5321/org.cocos2dx.ProjectName W/art: Suspending all threads took: 6.569ms

Your log seems missing a lot of information.
You could refer to cocos/audio/android/AudioDecoder.cpp, there are many ALOGV, but I didn’t see them in your log. Did you build the prebuilt library in release mode?
Please try to use debug mode to ensure the log is entire.

Sorry, stupid me, I was using wrong cocos2d-x directory (without this fix):

http://pastebin.com/V1JcZ3NB

Now it’s 500ms, better than 2000ms.

edit:
I guess it’s 500ms because of amount of logs. I’ll try in release mode in just a sec.

edit2:
In release mode:
217ms without playing.
233ms with playing.

Dunno why it’s longer than before fix, but fair enough it’s still very fast.
Looks like fix is working! You’re the best!
I’ll make some more testing before releasing though.

1 Like

@dumganhar do you have any Android 7 device?

I’ve got a new problem :frowning:

In one of my games I have a piano, so there’re several sounds. Now, when I click them a lot after 2-3 seconds new sounds stop playing and I’m getting:

D/cocos2d-x debug info: Fail to play key16.ogg cause by limited max instance of AudioEngine

That wasn’t happening with SimpleAudioEngine. I could play them as much as I wanted.

For other games it’s not an issue, but for this one is a game changer, because it kills the fun.

Is this possible to remove or increase that limit?

It’s not like I’m playing 100 different sounds, I have several, but they play for 2-3 seconds so it quickly hits the limit.

@dumganhar
what can we do about it?

I think my problem related with this issues

I think it was indeed time for a refactoring!
I had to stop some of my projects just because of laggy audio (even with preload, using the new AudioEngine), and I was experience some lag issues at my app startupp because of preloading about 150 short ogg sounds.

I just applied the “patch”, and it does fix the lag issue.
So I will just cross my finger, and publish this to the store :wink:

Did you update cmake for this feature too ? I prefer to use cmake but patch include just old android mk format! Thanks

Tried a Nexus 6 and the new audio engine works like a charm in Android 7. Seems a bit laggy with Android <= 6, i will keep trying with other devices

As I said in my previous post, I just crossed my finger, updated the AudioEngine and submitted my game to the Google Play store.

So far, I have no crash and my game has been tested on more than 70 different devices, and more than 15 different android version, from 4.0.4 to 7.1.1.
I only had very good reviews so I guess we can say your patch is a success :wink:

Good job!

Edit: In fact I just received a report with a bunch of crashes…
Here si the exception description:

ConcurrentModificationException (@HashMap$HashIterator:remove:810) {main}

I am not sure, could this be related to the AudioEngine ?

I saw this related post as well:
http://discuss.cocos2d-x.org/t/android-cocos2dxsound-has-concurrentmodificationexception/10461/8

So… does anyone have a solution for playing multiple sounds at the same time? Like making a piano, when using AudioEngine you have 16 tracks limit. If your sample has 2-3 seconds of duration it’s easy to break that 16 sounds playing at the same time limit. There’s no such a limitation in SimpleAudioEngine.

Yes, I have a Android 7.1 device (Nexus 5X).
Is there anything I could help you?

You could pass a profile structure to AudioEngine::play2d.


/** 
 * Play 2d sound.
 *
 * @param filePath The path of an audio file.
 * @param loop Whether audio instance loop or not.
 * @param volume Volume value (range from 0.0 to 1.0).
 * @param profile A profile for audio instance. When profile is not specified, default profile will be used.
 * @return An audio ID. It allows you to dynamically change the behavior of an audio instance on the fly.
 *
 * @see `AudioProfile`
 */
static int play2d(const std::string& filePath, bool loop = false, float volume = 1.0f, const AudioProfile *profile = nullptr);

class EXPORT_DLL AudioProfile
{
public:
    //Profile name can't be empty.
    std::string name;
    //The maximum number of simultaneous audio instance.
    unsigned int maxInstances;
    
    /* Minimum delay in between sounds */
    double minDelay;
    
    /**
     * Default constructor
     *
     * @lua new
     */
    AudioProfile()
    : maxInstances(0)
    , minDelay(0.0)
    {
        
    }
};

maxInstances indicates the max instances of sound could be played at the same time.
Currently, it should be less than or equal to 32.

You could also set the max instances by AudioEngine::setMaxAudioInstance(32) globally.
In that case, you doesn’t need to pass a profile structure every time.

@floboc, Good to know this patch works for your project. :slight_smile:

@jmclaveria, thank you for testing. Have you confirmed that there are still some laggy issues with Android <=6 devices?

@floboc, this is not related to AudioEngine, it’s probably an issue of SimpleAudioEngine.
BTW, if you’re using AudioEngine, why is there a crash in SimpleAudioEngine which uses Cocos2dxSound.java? Could you paste more about that crash stack?

No, I didn’t update cmake script. Could everyone help me update cmake file and send a patch to me?