Knowing when an audio.play() has finished

I have short audio files (3 secs long) that play() when the touch event is detected.
My problem is to not run the play() method again, until the current play() method has completed it’s process.

Here’s what I’ve attempted so far, with a variable that is set at zero.

local myFunSnd = audio.loadSound("myFunSound.mp3");  
local SndRunning = 0; --sound is off when first loaded  
  
function runSoundFunction(e)  
  
 if(e.phase=="began" and SndRunning==0) then  
 SndRunning = 1; --change variable to 1  
 audio.play(myFunSnd);  
 SndRunning = 0; --change back to 0, when sound is done  
 end  
  
end  
  
Runtime:addEventListener("touch", runSoundFunction)  

Hopefully the snippet above makes sense for what I’m attempting to accomplish.
This doesn’t work. So I’m wondering if anyone has a clever alternative. [import]uid: 154122 topic_id: 27294 reply_id: 327294[/import]

Actually I would do:

local isAudioPlaying = false  
  
local function donePlaying(event)  
 isAudioPlaying = false  
 return true  
end  
  
if not isAudioPalying then  
 isAudioPlaying = true  
 audio.play(track,{onComplete=donePlaying})  
end  

[import]uid: 19626 topic_id: 27294 reply_id: 110931[/import]

Yes, that is clever, indeed. It works like a charm.
Thanks robmiracle [import]uid: 154122 topic_id: 27294 reply_id: 110933[/import]

While Mr. Miracle’s code works perfectly fine, I was wondering if you could change your code to get it to work. Maybe switch the listen off of Runtime and add it onto myFunSnd. Then change your function on Line 6 to say if(e.phase ==“ended” etc.) I am not sure if that would work but if it does it allows you to then run other functions from the same call if you ever needed to.

PS. in your post rob on Line 8 you have a spelling error! Or it could be a Roy error, which is your brain is running faster than your fingers can type. it was named properly haha [import]uid: 50511 topic_id: 27294 reply_id: 110946[/import]

Oh it’s my typo. I’m not paid enough to actually compile and test my code snippets :slight_smile: :slight_smile: [import]uid: 19626 topic_id: 27294 reply_id: 110981[/import]