Touch Event + Audio Playback Crash

The following code reliably crashes my app when repeatedly tapping the screen quickly.

audio.reserveChannels( 1,2,3 )  
snd1 = audio.loadSound( "sound1.wav" )  
snd2 = audio.loadSound( "sound2.wav" )  
ambience = audio.loadStream( "ambience.wav" )  
audio.play( ambience, { channel=1, loops=-1, fadein=3000 })  
  
local function touchHandler( event )  
 if event.phase == 'began' then  
 audio.play( snd1, { channel=2, loops=-1})  
 elseif event.phase == 'ended' then  
 audio.stop( 2 )  
 if audio.isChannelPlaying( 3 ) then  
 audio.rewind( 3 )  
 else  
 audio.play( snd2, { channel=3 } )  
 end  
 end  
end  
  
display.getCurrentStage():addEventListener( 'touch', touchHandler )  

Android 2.3.3 Nexus One
adb logcat

V/Corona (27323): ****** ACTION_DOWN: CLEARING ********
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(1) *****
V/Corona (27323): ***** onTouchEvent(0) *****
V/Corona (27323): ****** ACTION_DOWN: CLEARING ********
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(2) *****
V/Corona (27323): ***** onTouchEvent(5) *****
V/Corona (27323): ***** onTouchEvent(2) *****
D/AndroidRuntime(27323): Shutting down VM
W/dalvikvm(27323): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(27323): FATAL EXCEPTION: main
E/AndroidRuntime(27323): java.lang.NullPointerException
E/AndroidRuntime(27323): at com.ansca.corona.CoronaActivity.onTouchEvent(Unknown Source)
E/AndroidRuntime(27323): at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
E/AndroidRuntime(27323): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
E/AndroidRuntime(27323): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
E/AndroidRuntime(27323): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
E/AndroidRuntime(27323): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27323): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(27323): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(27323): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27323): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(27323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(27323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(27323): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 96): Force finishing activity reelfernandes.Test/.MyCoronaActivity
I/WindowManager( 96): Setting rotation to 0, animFlags=1
I/ActivityManager( 96): Config changed: { scale=1.0 imsi=310/410 loc=en_US touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=108}
W/ActivityManager( 96): Activity pause timeout for HistoryRecord{407bc138 reelfernandes.Test/.MyCoronaActivity}
D/dalvikvm( 96): GC_EXPLICIT freed 133K, 42% free 6912K/11911K, external 7125K/7414K, paused 187ms
I/InputDispatcher( 96): Application is not responding: Window{40781798 reelfernandes.Test/reelfernandes.Test.MyCoronaActivity paused=false}. 5179.1ms since event, 5002.2ms since wait started
I/InputDispatcher( 96): Dropping event because the pointer is not down.
I/InputDispatcher( 96): Dropping event because the pointer is not down.
I/InputDispatcher( 96): Dropping event because the pointer is not down.
I/WindowManager( 96): Input event dispatching timed out sending to reelfernandes.Test/reelfernandes.Test.MyCoronaActivity
I/ActivityManager( 96): Crashing app skipping ANR: ProcessRecord{40833dd8 27323:reelfernandes.Test/10035} keyDispatchingTimedOut
W/ActivityManager( 96): Activity destroy timeout for HistoryRecord{407bc138 reelfernandes.Test/.MyCoronaActivity}
[import]uid: 4596 topic_id: 7334 reply_id: 307334[/import]

Most likely because you’re sending audio api calls very often in that case. It _is_ a bug but one workaround might be to use a timer.

So on tap do:

if TimerActive == false then
TimerActive = true
timer.performWithDelay(50, setSoundState)
end
sound1state = “play”
sound2state = “stop”
{etc etc}

then in setSoundState do the actual audio.play/rewind/stop calls depending on the sound1state/sound2state etc, and set TimerActive to false.
[import]uid: 8872 topic_id: 7334 reply_id: 26012[/import]

Thanks for the suggested workaround kam. If it’s not fixed in time I’m going to run with that idea. Perhaps using a timer to add a buffer between audio playback & rapid touch events. That said, I’m only assuming that audio playback is causing the crash, it’s possible that the rapid touch events alone cause the crash, I haven’t tested it w/out audio.

local timerActive = false

onTimer( event )
timerActive = false
end

onTouch( event )
if not timerActive then
doSoundStuff()
timerActive = true
timer.performWithDelay( 50, onTimer )
end
end [import]uid: 4596 topic_id: 7334 reply_id: 26055[/import]

What version of corona are you using? 268 has issues with touch events which were fixed in the daily’s. However that broke performance on droid 2,X and Nexus S (and Spica… the list gets bigger every day!) [import]uid: 8872 topic_id: 7334 reply_id: 26059[/import]

I’ve been grabbing daily build every few days, right now I’m on 308. Interesting, looking through the logcat posted above, I don’t see anything mentioning audio api. Do you know if there’s a way to output the line causing the crash. I suppose I could try printing output between each line. [import]uid: 4596 topic_id: 7334 reply_id: 26176[/import]

I think u’d have to print between each line. [import]uid: 8872 topic_id: 7334 reply_id: 26180[/import]

Confirmed that the crash is caused by the audio API [import]uid: 4596 topic_id: 7334 reply_id: 26388[/import]

+1 on this issue. I’m only getting a crash on my Android build…iOS seems to work fine. This code crashes the entire app upon touch of the button. Everything is perfect in the simulator though!

[code]local soundbutton = ui.newButton{
default = “soundbutton.png”,
over = “soundbuttonover.png”,
x = 55,
y = 310,
}

local function playsound ( event )
if event.phase == “ended” then
media.playSound( “sound.mp3” )
end
end
soundbutton:addEventListener(“touch”,playsound)[/code]

Anyone see anything wrong with the code or is this now a reported bug? Running build 316. [import]uid: 20687 topic_id: 7334 reply_id: 28462[/import]

Fixed my problem: evidently it was something in the ui.lua file that was crashing my android build. Removed the button over state and code and it didn’t crash.

If anyone else can get touch button over states to work with mp3 files on android device builds let me know! [import]uid: 20687 topic_id: 7334 reply_id: 29189[/import]