How to implement media api for lagfree audio ?

Hi

I’m working on a space shooter. When touching the engine buttons, a thrust sound shall continue as long the button is touched. I tried audio api, but the lag (on a s3) is intolerable. I read that media api is faster, but have problems implementing it. When I use media event, the sound starts, but I cant stop, pause or start it anymore. The used thrust.wav is a white noise, 20 secs long. How would you do this? Here is the needed structure;

If touch began
Play/resume sound
Elseif ended or cancelled
Pause sound
End

Is this possible with media api?

Thanks for your help!
Olivier

This might be helpful: http://forums.coronalabs.com/topic/56269-android-audio-latency-the-10-ms-problem/

The media.* really only works well with short clips.  20 seconds may be too long.   As the link above points to, Android simply cannot delivery sound at a speed fast enough without directly accessing the NDK and even then that only works on the right hardware Android version combinations.  audio* will have the most lag.   media.* has many limitations but gets you to the quickest speed that we can get too.

Rob

Hi

Thanks for your replies! Do you have a example how to implement the media api for the given task? I’m really stuck and have some weird effects like crashes when the audio file has come to an end (and I suppose media.play tries to play it further) or the sound doesn’t stop anymore.

Would you do it like this?: (This isn’t working…)

local thrust = media.newEventSound( "thrust.mp3" ) local function onObjectTouch( event ) if event.phase == "began" then media.playEventSound( thrust ) elseif event.phase == "ended" or event.phase == "cancelled" then media.stopSound(thrust) end

Thanks

Olivier

After some trying I found the solution. With loop activitated it runs forever and you can simply set the volume up and down.

Best regards

Olivier

media.playSound( "thrust.wav", true ) media.setSoundVolume( 0 ) local function onObjectTouch( event )    if event.phase == "began" then       media.setSoundVolume( 0.5 )    elseif event.phase == "ended" or event.phase == "cancelled" then       media.setSoundVolume( 0 )    end end

This might be helpful: http://forums.coronalabs.com/topic/56269-android-audio-latency-the-10-ms-problem/

The media.* really only works well with short clips.  20 seconds may be too long.   As the link above points to, Android simply cannot delivery sound at a speed fast enough without directly accessing the NDK and even then that only works on the right hardware Android version combinations.  audio* will have the most lag.   media.* has many limitations but gets you to the quickest speed that we can get too.

Rob

Hi

Thanks for your replies! Do you have a example how to implement the media api for the given task? I’m really stuck and have some weird effects like crashes when the audio file has come to an end (and I suppose media.play tries to play it further) or the sound doesn’t stop anymore.

Would you do it like this?: (This isn’t working…)

local thrust = media.newEventSound( "thrust.mp3" ) local function onObjectTouch( event ) if event.phase == "began" then media.playEventSound( thrust ) elseif event.phase == "ended" or event.phase == "cancelled" then media.stopSound(thrust) end

Thanks

Olivier

After some trying I found the solution. With loop activitated it runs forever and you can simply set the volume up and down.

Best regards

Olivier

media.playSound( "thrust.wav", true ) media.setSoundVolume( 0 ) local function onObjectTouch( event )    if event.phase == "began" then       media.setSoundVolume( 0.5 )    elseif event.phase == "ended" or event.phase == "cancelled" then       media.setSoundVolume( 0 )    end end