[Android] Audio Decoding Issues Discussion

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?

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…