New Android Audio Lag

@tomekgt.  I know this is frustrating.  But if you google about Android OpenSL Audio lag or latency, you will find this doesn’t just affect Corona SDK.  Google fixed it in Android 4.1 and improved it in 4.2.  It also is very device dependent.  Even with these fixes, some devices have less lag than others.

I know Rob. It is frustrating. It’s just such a simple thing. Why did Google bother putting having OpenSL audio if it was so crap? If it could work lag free on the S1 then it should have been possible for Google to fix it everywhere or work with handset manufacturers to do so. It will be at least 2 years before we can  assume that most people have 4.2.2 because I can confirm that 4.2.1 does not fix it at all on the S3. That’s assuming 4.2.2 fixes it which according to ingemar, it doesn’t fully.

I have a feeling that it might not always be Google’s fault. One thing that complicates the matter is that Android device manufacturers implement their own hardware and software modifications. This has the potential to either improve things, or make things worse.

As a side-note, I have a Samsung Galaxy Player running Android 2.3.6 (which essentially is a Galaxy S1 without the phone) and it had a severe lag (~500ms) until OpenSL ES was tweaked in Build 945. There’s still a lag, but it’s almost on-par with Android 4.2 now.

I am not sure how are you assuming that this is a google problem.

Most android games do not show any lag even prior to 4.1

Adobe AIR does not show any lage on android.

We are facing it with Corona.

I wonder as well how most android games don’t have any sound lag.  Are these all games that are built natively with the Android SDK? Either way this problem sucks soooo bad.  Just released a new app and people already are complaining about the audio lag… sigh

Everyone,

The audio latency issue is an issue on Android’s end.  It’s unfortunately a famous issue that all native Android developers have had to deal with and Google did not address it until Android OS 4.2 (Jelly Bean), which you can see in their release notes below under the “Low-latency audio” section.  Google made this low latency improvement to their OpenSL library, which is what Corona’s “audio” API leverages.

   http://developer.android.com/about/versions/jelly-bean.html


Edit:  Google’s Android 4.2 and newer OS versions does not *solve* the audio latency issue on all devices as mentioned above and in Google’s release notes.  Many of the newest Android devices still have a latency issue, and because of this, Google has left this issue “open” in their issue tracker (link below).  Because of this, you should still heed my advice in the paragraphs below.

   https://code.google.com/p/android/issues/detail?id=3434


For older Android OS versions, Google documents that their fastest audio API is their SoundPool class in Java, which is what Corona’s media.playEventSound() API uses.  Google’s documentation for this can be found via the link below under paragraph 3.  The reason this audio API is faster is because Android loads the sound directly to the audio hardware.

   http://developer.android.com/reference/android/media/SoundPool.html

Regarding Corona’s “audio” API, realize that this is a streaming and audio processing API.  That is, it processes audio data by applying fades and volume effects and then mixes all of those audio channels into a single stream at “runtime” before delivering it to the audio hardware.  This audio processing/mixing is done via a middle layer called OpenAL.  On iOS, OpenAL is directly supported and hardware accelerated.  On Android, it is CPU bound and is *not* hardware accelerated.  So, the more audio channels that have to be “mixed”, the more work the CPU has to do before delivering the single mixed audio stream to the hardware.  This adds to the latency.  So, if you want less latency, then you need to use less audio channels.

That said, the best solution is to do this:

  • Play short sound effect clips via the media.playEventSound() API.

  • Play music or long sound clips via the “audio” API.

Doing the above will make your app’s audio playback on par with other apps that you’ve seen, because the truth is that Android has several audio APIs (SoundPool, AudioTrack, OpenSL) and each have their own advantages and disadvantages.  There is no single easy to use audio API on Android on the native side.  At least not until Android 4.2 with the OpenSL library.

Also, ingemar is correct.  Sometimes the audio latency is caused by the device manufacturer.  For example, the 1st generation Kindle Fire has horrible audio latency and there is no work-around.  Try playing Angry Birds on that particular device.  The audio lag is about 300-500 milliseconds.

Anyways, that’s my 2 cents.  I hope this helps!

Hi Joshua, playing a lot of short sounds using media.playEventSound like for example coins being consumed by a player will produce a lag on android.

I was wondering if there is a fix for this problem? Our app has 1 second delay on Android devices because of Corona. 

What can we do about it? 

I’m having the exact same problem.  I’m trying to provide some form of click feedback when a user clicks on (virtual) keys, but the lag is so bad even with a 45ms audio file, that it’s completely useless.  I’d love to use the system.vibrate function for this, but like many other items, this too has also been very poorly implemented by Corona.  There is no way to set the strength of the vibration and it appears that Corona has it cranked to the maximum by default which emits a loud and annoying buzzing sound on most tablets and phones that we’ve tried. I need basic tactile feedback, something that all the competing apps to mine offer, and it’s an embarrassment that this can’t be done with Corona.

These are extremely basic and fundamental functions (not just nice-to-haves) and it’s difficult to understand why Corona has ignored these for so long – especially as this is primarily a gaming platform. Corona seriously needs to dedicate a developer to fix a few very fundamental functions like system.vibrate (strength), audio.play (eliminate lag) and (perhaps the most difficult to understand) provide the ability to programmatically copy/read text to/from the clipboard (and after 3 years of countless users requesting this).  How could anyone take Corona seriously as a platform for developing business apps, when text can’t even be copied/pasted to/from the clipboard?  This functionality for plain text is only about 3 to 10 lines of native code on each respective platform.

Google did fix it in Android 4.1 (I think).

I’ve got a Nexus 7 with Android 4.2.2, and the audio lag is as good as gone. Compared to iOS which has no lag whatsoever, the Android device still has a short lag, although barely noticeable during gameplay unless you look/listen for it.

Google still has some work to do to get it completely eliminated…

> I’ve got a Nexus 7 with Android 4.2.2, and the audio
> lag is as good as gone. … the Android device still

> has a short lag,

If you’re just playing a music file, “a short lag” might be equatable to “good as gone,” but for games where a laser sound (‘pew pew pew’) is supposed to sync up with the user hitting a button or a business app where key-click sounds should coincide with pressing of keys, a 500ms to 750ms lag is completely unacceptable.

I’m *not* talking about music files, I’m talking about games with a laser sounds (‘pew pew pew’) and the like.

I’d estimate the lag in the latest versions of Android to be <100ms.

…and this is an Android problem, not Corona SDK.

I don’t understand how google could have messed up so much. But on the other hand I still feel that Corona is somewhat at fault. I had no lag on my s1 so it was possible before 4.2.2. Anyway, it’s kind of useless since most phones aren’t on the latest android version.

I just have to accept sounds cutting off sometimes with the media api, that’s all.

@tomekgt.  I know this is frustrating.  But if you google about Android OpenSL Audio lag or latency, you will find this doesn’t just affect Corona SDK.  Google fixed it in Android 4.1 and improved it in 4.2.  It also is very device dependent.  Even with these fixes, some devices have less lag than others.

I know Rob. It is frustrating. It’s just such a simple thing. Why did Google bother putting having OpenSL audio if it was so crap? If it could work lag free on the S1 then it should have been possible for Google to fix it everywhere or work with handset manufacturers to do so. It will be at least 2 years before we can  assume that most people have 4.2.2 because I can confirm that 4.2.1 does not fix it at all on the S3. That’s assuming 4.2.2 fixes it which according to ingemar, it doesn’t fully.

I have a feeling that it might not always be Google’s fault. One thing that complicates the matter is that Android device manufacturers implement their own hardware and software modifications. This has the potential to either improve things, or make things worse.

As a side-note, I have a Samsung Galaxy Player running Android 2.3.6 (which essentially is a Galaxy S1 without the phone) and it had a severe lag (~500ms) until OpenSL ES was tweaked in Build 945. There’s still a lag, but it’s almost on-par with Android 4.2 now.

I am not sure how are you assuming that this is a google problem.

Most android games do not show any lag even prior to 4.1

Adobe AIR does not show any lage on android.

We are facing it with Corona.

I wonder as well how most android games don’t have any sound lag.  Are these all games that are built natively with the Android SDK? Either way this problem sucks soooo bad.  Just released a new app and people already are complaining about the audio lag… sigh

Everyone,

The audio latency issue is an issue on Android’s end.  It’s unfortunately a famous issue that all native Android developers have had to deal with and Google did not address it until Android OS 4.2 (Jelly Bean), which you can see in their release notes below under the “Low-latency audio” section.  Google made this low latency improvement to their OpenSL library, which is what Corona’s “audio” API leverages.

   http://developer.android.com/about/versions/jelly-bean.html


Edit:  Google’s Android 4.2 and newer OS versions does not *solve* the audio latency issue on all devices as mentioned above and in Google’s release notes.  Many of the newest Android devices still have a latency issue, and because of this, Google has left this issue “open” in their issue tracker (link below).  Because of this, you should still heed my advice in the paragraphs below.

   https://code.google.com/p/android/issues/detail?id=3434


For older Android OS versions, Google documents that their fastest audio API is their SoundPool class in Java, which is what Corona’s media.playEventSound() API uses.  Google’s documentation for this can be found via the link below under paragraph 3.  The reason this audio API is faster is because Android loads the sound directly to the audio hardware.

   http://developer.android.com/reference/android/media/SoundPool.html

Regarding Corona’s “audio” API, realize that this is a streaming and audio processing API.  That is, it processes audio data by applying fades and volume effects and then mixes all of those audio channels into a single stream at “runtime” before delivering it to the audio hardware.  This audio processing/mixing is done via a middle layer called OpenAL.  On iOS, OpenAL is directly supported and hardware accelerated.  On Android, it is CPU bound and is *not* hardware accelerated.  So, the more audio channels that have to be “mixed”, the more work the CPU has to do before delivering the single mixed audio stream to the hardware.  This adds to the latency.  So, if you want less latency, then you need to use less audio channels.

That said, the best solution is to do this:

  • Play short sound effect clips via the media.playEventSound() API.

  • Play music or long sound clips via the “audio” API.

Doing the above will make your app’s audio playback on par with other apps that you’ve seen, because the truth is that Android has several audio APIs (SoundPool, AudioTrack, OpenSL) and each have their own advantages and disadvantages.  There is no single easy to use audio API on Android on the native side.  At least not until Android 4.2 with the OpenSL library.

Also, ingemar is correct.  Sometimes the audio latency is caused by the device manufacturer.  For example, the 1st generation Kindle Fire has horrible audio latency and there is no work-around.  Try playing Angry Birds on that particular device.  The audio lag is about 300-500 milliseconds.

Anyways, that’s my 2 cents.  I hope this helps!