[Android] Audio Decoding Issues Discussion

@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?

Solved it, a function was missing and that was causing the lag (getSDKVersion from JNIHelper). Now it works fine in all devices :slight_smile:

Great. :grinning:

I don’t use SimpleAudioEngine at all.
I was wondering if this could be related, for instance if the new AudioEngine used hashmaps as well.
I have no more info on these crahes, I only got them through analytics and had no similar crash on my personal devices…

AudioEngine doesn’t use any java code, it’s implemented in native (cpp). So this crash seems not to be related to AudioEngine.
BTW, could you provide the whole crash stack log? And are you make sure this crash appear in the version of applying my patch?

Here is the stack trace I get:

java.util.ConcurrentModificationException

at java.util.HashMap$HashIterator.nextEntry(HashMap.java:851)
at java.util.HashMap$EntryIterator.next(HashMap.java:891)
at java.util.HashMap$EntryIterator.next(HashMap.java:890)
at org.cocos2dx.lib.Cocos2dxSound.setEffectsVolume(Cocos2dxSound.java:250)
at org.cocos2dx.lib.Cocos2dxHelper.setEffectsVolume(Cocos2dxHelper.java:419)
at org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method)
at org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:104)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1553)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1253)

@dumganhar So I am not the only one getting hash map concurrent modification error, please consider above post.

@atin_agarwal2 do you have a way to reproduce this error ? Do you confirm that you are using the new audio engine too ?

Cocos2dxSound is a class used by SimpleAudioEngine, not AudioEngine. AudioEngine uses cpp code only.