Android Touch Lag

That’s very cool if they did finally fix audio latency on Android.  But I read somewhere that 4.2 and above is currently installed on something like only 7% of Android devices, so realistically most people are still going to experiencing that audio delay.  Maybe the media library functions are the answer to your issue, mobiledev5.

-Stephen

Hello guys, as I said there is an Android App which uses same sound files, but it responses much faster, realy fast.

Can it be because of, corona’s minimum android sdk version 2.2? 

Maybe if we build it with newer version, it’s performance may change?

How can we try this?

Hi all,

Just to provide some more details on this:

  1. Google resolved the audio latency issue in Android 4.2. It’s documented in their release notes below under the “Low-latency audio” section. It was resolved in Android’s OpenSL library which is what Corona has used since at least build #971.

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

  1. For older Android OS versions, Google documents that their SoundPool class in Java has the least audio latency. This is what Corona’s “media.playEventSound()” function uses. The limitation being that it is only fit for short sound effects.

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

So this being said, you may be able to successfully detect the platform version using system.getInfo(“platformVersion”), carefully parse/read the string to make sure you’re sensing 4.2 and above, and then use the audio library functions to play on this device, or default to the media library functions otherwise.

Brent

Thank you Brent, media.playEventSound() worked very well, but how can I stop it, how can I loop it?

Hi @Ivatek Studio,

If you want to loop the sound (or stop it), you may consider using “media.playSound()” instead. This has the ability to loop the file, as well as stop the file using “media.stopSound()”. Please inspect the details for these APIs here:

http://docs.coronalabs.com/api/library/media/index.html

Sincerely,

Brent Sorrentino

Thanks for the tips  Brent.  After some messing around I was able to continue using the new audio api for music tracks (since latency was not distracting for music) and use the old media.playEventSound for sound effects to significantly reduced latency for them.  I do this only on devices detected running Android 4.1 or less, otherwise I use the new audio API for everything.

In case anyone else goes down this path, I had to make sure to preload all sound effects using the old api with media.newEventSound( [soundfile] ), otherwise, on the device, the sound effects would not play more often than they would.  Testing on the simulator without preloading worked fine, so don’t be fooled!

And here is a code snippet i use to detect the Android version #:

versionNumber = tonumber(string.sub(system.getInfo("platformVersion"),1,3))

This will set versionNumber to 4.1 if the platformVersion is, say 4.1.2.  So if versionNumber <  4.2 then I know to use the old audio API.

-Stephen

Hello thanks,

In your documentation it says that we can only open sound file at a time. 

I need simultaneus playing file as in soundpool. And also I need to stop any instance of sound.

Because of these things, I chose audio library, but it has latency problems.

We develop virtual instruments, so we need low latency, media.newEventSound can help us but we cant use it simultaneus playings and loop.

This is very bad for us.

What are we going to do?

Hi @Ivatek Studio,

The “media.playEventSound()” function actually does support multiple sounds at the same time on Android. This API is perfect for playing sound effects as quickly as possible on that platform. It does not support looping, but you can do this yourself via a timer.

Sincerely,

Brent

Thank you for posting this thread.  Been frustrated with the audio lag since 2011 when I started with Corona =)  Going to give your suggestions a go very soon. Thanks!

Thank you for posting this thread.  Been frustrated with the audio lag since 2011 when I started with Corona =)  Going to give your suggestions a go very soon. Thanks!

Hi I want to add to this post regarding Android Touch Lag. It seems the focus was on the media but from my personal experience I experienced lag on both the Media and Touch Events. My app works great on IOS but on Android when you touch the screen to jump there is a slight delay (lag) for the character to jump which doesn’t happen on IOS. The media is also an issue but im aware of the bug and glad it got fixed on 4.2 but regarding the Touch event anyone else experienced lag? I also know is not device related issue because we tested on Samsung Galaxy S4 and it has slightly less lag as older Android Devices but it still there. It just seems older device lag more but even newest device have lag and on IOS not even iPhone 4 lags. Would like to see if other people experiencing lag on Android devices with Touch Events and how they work around it!

Here is my jumping function:

local function fish\_jump(event) if event.phase == "began" and game\_ready == true and game\_lose == false then fish\_circle:setLinearVelocity(0, -300) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(soundOn == true)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audio.play(fly\_sound) &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;end end touch\_square:addEventListener("touch", fish\_jump) &nbsp;

Hi I want to add to this post regarding Android Touch Lag. It seems the focus was on the media but from my personal experience I experienced lag on both the Media and Touch Events. My app works great on IOS but on Android when you touch the screen to jump there is a slight delay (lag) for the character to jump which doesn’t happen on IOS. The media is also an issue but im aware of the bug and glad it got fixed on 4.2 but regarding the Touch event anyone else experienced lag? I also know is not device related issue because we tested on Samsung Galaxy S4 and it has slightly less lag as older Android Devices but it still there. It just seems older device lag more but even newest device have lag and on IOS not even iPhone 4 lags. Would like to see if other people experiencing lag on Android devices with Touch Events and how they work around it!

Here is my jumping function:

local function fish\_jump(event) if event.phase == "began" and game\_ready == true and game\_lose == false then fish\_circle:setLinearVelocity(0, -300) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(soundOn == true)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audio.play(fly\_sound) &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;end end touch\_square:addEventListener("touch", fish\_jump) &nbsp;

Is this still the advice for Android - to use this media.playEventSound()?

I’m testing on Samsung Galaxy  Tab 4 (Android 4.4 KitKat) but the audio lag is just slow enough that I can’t play in time rhythmically and my app needs this ability (e.g. to play in time with the music track).

I am using .ogg files and have tried the media.playEventSound() as well as media.playSound() but I’m not getting any sound at all within the Simulator.   Just wondering if this thread is outdated and hence these APIs are also.

Using Corona Simulator Version 2014.2189 (2014.3.6)

Everything plays with audio.play just not with above APIs.

Hello @rshanlon,

Unless you are only targeting 4.4 or above (unlikely), then you should still use these media.* APIs on Android. Did you correctly pre-load your audio files using media.newEventSound()? How long are your audio files and what size in KB?

Best regards,

Brent

Yes, with **media.playEventSound()  **I put this inside my functions to play the audio and I used media.newEventSound() in the Show/Did area of Composer to load them.  But no sound.    My widgets are calling the functions that have media.playEventSound().  

Each short sound .ogg file (13kb) is .3 to .5 seconds short.  So, the ones I’m trying to use with this API are all under a second long.

Now, that said, I also have music files that go for 1 to 2 minutes in length but I’m using the audio.play API for those.

I just need the speed of touch with the shorter sounds.

Do I have these placed in correct area of Composer?  My Functions are all before CreateScene and media.newEventSound() are in Show/Did.   I also tested them placed in Show/Will but no sound.  And I also tested them in Create:Scene but Simulator crashes when I tap on the widget.

Here’s a sample for one of the audio files and the three parts involved in Composer.  The first two functions are placed before scene:create

[lua]

local function play_a_btn ( event )

if ( “began” == event.phase ) then

media.playEventSound(a_sound)

end

end

local function setUpDisplayLetters (grp)

local a_btn = widget.newButton

{

left = centerX - 300,

top = centerY - 270,

defaultFile = “images/a_red_btn_v.png”,

overFile = “images/a_red_btnPush_v.png”,

onEvent = play_a_btn

}

grp:insert(a_btn)

end 

function scene:show( event )

        local sceneGroup = self.view

        local phase = event.phase

        if ( phase == “will” ) then

        composer.loadScene( “consonants”, false, options )

        composer.loadScene( “menu”, false, options )

–Called when the scene is still off screen (but is about to come on screen)

        elseif ( phase == “did” ) then

–SOUNDS

a_sound = media.newEventSound( “sounds/a_sound.ogg” )

   

      end

end

[/lua]

I have done more tests.  In fact, I’ve stripped the coding down to as basic as you can get.

I’m using Android 4.4  (So this talk that Android “solved” it in 4.2 is false from what I’m experiencing)

Results:  ***  terrible audio latency with audio.play  with ogg files.

     (Even with the widget set to  onPress  or  onEvent ​ the latency is unacceptable.)

***Audio doesn’t play using  media.playEventSound() with ogg files.

This is my incredibly stripped down code not even using Composer.  I have loaded it on my Samsung Tab 4.  If I walk in-time with a steady rhythm and I tap on this widget the sound always plays on the upbeat (i.e. when my foot is in the air not on the ground).    Maybe that’s greater than 500ms latency.

This code below uses audio.play but the results are worse with media.playEventSounds() because the sound doesn’t play.

[lua]

local widget = require(“widget”)

local centerX = display.contentWidth / 2

local centerY = display.contentHeight / 2

local a_sound = audio.loadSound( “a_sound.ogg” )

local play_a_btn = function ( event )

  if ( “began” == event.phase ) then

  audio.play(a_sound)

  end

end

local a_btn = widget.newButton

  {

    left = centerX - 300,

    top = centerY - 270,

    defaultFile = “a_red_btn_v.png”,

    overFile = “a_red_btnPush_v.png”,

    onPress = play_a_btn

  }

   

[/lua]

My final test will be to substitute the.ogg file for an MP3 file using audio.play and see what happens.   I got m4a files to work on the short files but with longer music files, they wouldn’t play back on Android 4.4.  Not sure why.

Update:

Results with MP3 and audio.play = latency still is terrible!

MP3 with media.playEventSound = a little better but still behind the beat

[lua]

local widget = require(“widget”)

local centerX = display.contentWidth / 2

local centerY = display.contentHeight / 2

local a_sound = media.newEventSound( “a_sound.mp3” )

local play_a_btn = function ( event )

  if ( “began” == event.phase ) then

  media.playEventSound(a_sound)

  end

end

local a_btn = widget.newButton

  {

    left = centerX - 300,

    top = centerY - 270,

    defaultFile = “a_red_btn_v.png”,

    overFile = “a_red_btnPush_v.png”,

    onPress = play_a_btn

  }

[/lua]

Update:

Changing “onPress” to “onEvent” made slightly better results.

Update:

Short audio files, I’m using m4a with media.playEventSound

(performance was about the same as MP3 but no licensing restrictions like MP3 has)

Note: made sure to use onEvent instead of onPress. 

longer music files, I’m using ogg files with audio.play.

(Remember ogg didn’t work with media.playEventSound but works fine with audio.play—m4a didn’t work for me when using these longer music files with audio.play on Android 4.4)  No clue why but these are my final settings to release this Android app.  

The latency is still there but much less and close enough that you can play along with music in-time relativity well.

Is this still the advice for Android - to use this media.playEventSound()?

I’m testing on Samsung Galaxy  Tab 4 (Android 4.4 KitKat) but the audio lag is just slow enough that I can’t play in time rhythmically and my app needs this ability (e.g. to play in time with the music track).

I am using .ogg files and have tried the media.playEventSound() as well as media.playSound() but I’m not getting any sound at all within the Simulator.   Just wondering if this thread is outdated and hence these APIs are also.

Using Corona Simulator Version 2014.2189 (2014.3.6)

Everything plays with audio.play just not with above APIs.

Hello @rshanlon,

Unless you are only targeting 4.4 or above (unlikely), then you should still use these media.* APIs on Android. Did you correctly pre-load your audio files using media.newEventSound()? How long are your audio files and what size in KB?

Best regards,

Brent

Yes, with **media.playEventSound()  **I put this inside my functions to play the audio and I used media.newEventSound() in the Show/Did area of Composer to load them.  But no sound.    My widgets are calling the functions that have media.playEventSound().  

Each short sound .ogg file (13kb) is .3 to .5 seconds short.  So, the ones I’m trying to use with this API are all under a second long.

Now, that said, I also have music files that go for 1 to 2 minutes in length but I’m using the audio.play API for those.

I just need the speed of touch with the shorter sounds.

Do I have these placed in correct area of Composer?  My Functions are all before CreateScene and media.newEventSound() are in Show/Did.   I also tested them placed in Show/Will but no sound.  And I also tested them in Create:Scene but Simulator crashes when I tap on the widget.

Here’s a sample for one of the audio files and the three parts involved in Composer.  The first two functions are placed before scene:create

[lua]

local function play_a_btn ( event )

if ( “began” == event.phase ) then

media.playEventSound(a_sound)

end

end

local function setUpDisplayLetters (grp)

local a_btn = widget.newButton

{

left = centerX - 300,

top = centerY - 270,

defaultFile = “images/a_red_btn_v.png”,

overFile = “images/a_red_btnPush_v.png”,

onEvent = play_a_btn

}

grp:insert(a_btn)

end 

function scene:show( event )

        local sceneGroup = self.view

        local phase = event.phase

        if ( phase == “will” ) then

        composer.loadScene( “consonants”, false, options )

        composer.loadScene( “menu”, false, options )

–Called when the scene is still off screen (but is about to come on screen)

        elseif ( phase == “did” ) then

–SOUNDS

a_sound = media.newEventSound( “sounds/a_sound.ogg” )

   

      end

end

[/lua]