Android Touch Lag

Hello, how can I make faster touch responses in Android? 

It is very slow from IOS.

Hello @mobiledev5,

You will need to provide many more details for the community or staff to help you. We don’t know what your code looks like, what device(s) you’re testing on, what your settings look like, etc. etc. Please provide as many details as you can so the community can assist you.

Best regards,

Brent Sorrentino

Hello sorry about my late reply, 

I tested this code in galaxy s2,s3 etc.

But it responses very slow. But same code is very good in iphone 3gs, iphone 4 etc.

And also there is an android app(not corona) that uses the same sound files, it is much faster in android.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- display.setStatusBar( display.HiddenStatusBar ) system.activate( "multitouch" ) local loadedSounds={} local function playSound(id) audio.stop(id) audio.play( loadedSounds[id] , { channel=id}) print("channel:"..id) end local lastMoved= nil local function playEvent(self,event) if event.phase=="began" then playSound(event.target.id) end if event.phase=="moved" then if lastMoved ~= event.target.id then playSound(event.target.id) lastMoved = event.target.id end end end local function newButton(\_id,\_x,\_y,\_soundFilePath,\_playEvent) local button = display.newRect(\_x, \_y, 50, 40) button.id=\_id button.x=\_x button.y=\_y button.touch=\_playEvent button:addEventListener("touch",button) loadedSounds[\_id] = audio.loadSound(\_soundFilePath) return button end local button1 =newButton(1,37,146,"notalar/1-do1.wav",playEvent) local button2 =newButton(2,94,146,"notalar/2-re.wav",playEvent) local button3 =newButton(3,152,146,"notalar/3-mi.wav",playEvent) local button4 =newButton(4,209,146,"notalar/4-fa.wav",playEvent) local button5 =newButton(5,267,146,"notalar/5-sol.wav",playEvent) local button6 =newButton(6,324,146,"notalar/6-la.wav",playEvent) local button7 =newButton(7,382,146,"notalar/7-si.wav",playEvent) local button8 =newButton(8,439,146,"notalar/8-do2.wav",playEvent)

Hi @mobiledev5,

Are these large sound files? Are you attempting to click one of the buttons before the sound may be loaded? Ideally, you should pre-load all sounds before even allowing them to be played. You might also consider using a more compressed format like .ogg or even .mp3 (although this one caries with it the annoying licensing issues).

Sincerely,

Brent Sorrentino

It’s probably not the touch response that is slow.  However, there is a very noticeable audio lag on Android devices that has been pointed out on these forums for ages.  According to the Corona engineers this is not something that Corona Labs can fix as apparently it’s a defect in the Android spec. itself.  I perceive the audio delay in my own app on Android at around 300-500 ms, which is noticeably horrible.  On iOS, even on old 3GS, I don’t notice any audio delay.

According to my research, the Android latency issue was fixed in 4.2 and above. On 2.2+ devices, you still may consider using the Corona media library functions to play sounds; that should resolve the latency issues on devices running those versions of Android.

Best regards,

Brent

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

Hello @mobiledev5,

You will need to provide many more details for the community or staff to help you. We don’t know what your code looks like, what device(s) you’re testing on, what your settings look like, etc. etc. Please provide as many details as you can so the community can assist you.

Best regards,

Brent Sorrentino

Hello sorry about my late reply, 

I tested this code in galaxy s2,s3 etc.

But it responses very slow. But same code is very good in iphone 3gs, iphone 4 etc.

And also there is an android app(not corona) that uses the same sound files, it is much faster in android.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- display.setStatusBar( display.HiddenStatusBar ) system.activate( "multitouch" ) local loadedSounds={} local function playSound(id) audio.stop(id) audio.play( loadedSounds[id] , { channel=id}) print("channel:"..id) end local lastMoved= nil local function playEvent(self,event) if event.phase=="began" then playSound(event.target.id) end if event.phase=="moved" then if lastMoved ~= event.target.id then playSound(event.target.id) lastMoved = event.target.id end end end local function newButton(\_id,\_x,\_y,\_soundFilePath,\_playEvent) local button = display.newRect(\_x, \_y, 50, 40) button.id=\_id button.x=\_x button.y=\_y button.touch=\_playEvent button:addEventListener("touch",button) loadedSounds[\_id] = audio.loadSound(\_soundFilePath) return button end local button1 =newButton(1,37,146,"notalar/1-do1.wav",playEvent) local button2 =newButton(2,94,146,"notalar/2-re.wav",playEvent) local button3 =newButton(3,152,146,"notalar/3-mi.wav",playEvent) local button4 =newButton(4,209,146,"notalar/4-fa.wav",playEvent) local button5 =newButton(5,267,146,"notalar/5-sol.wav",playEvent) local button6 =newButton(6,324,146,"notalar/6-la.wav",playEvent) local button7 =newButton(7,382,146,"notalar/7-si.wav",playEvent) local button8 =newButton(8,439,146,"notalar/8-do2.wav",playEvent)

Hi @mobiledev5,

Are these large sound files? Are you attempting to click one of the buttons before the sound may be loaded? Ideally, you should pre-load all sounds before even allowing them to be played. You might also consider using a more compressed format like .ogg or even .mp3 (although this one caries with it the annoying licensing issues).

Sincerely,

Brent Sorrentino

It’s probably not the touch response that is slow.  However, there is a very noticeable audio lag on Android devices that has been pointed out on these forums for ages.  According to the Corona engineers this is not something that Corona Labs can fix as apparently it’s a defect in the Android spec. itself.  I perceive the audio delay in my own app on Android at around 300-500 ms, which is noticeably horrible.  On iOS, even on old 3GS, I don’t notice any audio delay.

According to my research, the Android latency issue was fixed in 4.2 and above. On 2.2+ devices, you still may consider using the Corona media library functions to play sounds; that should resolve the latency issues on devices running those versions of Android.

Best regards,

Brent