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.