Play sounds one after another.

I’m new to this and this is probably a very basic question.
For some reason I can’t figure out what is the proper way to play
sounds one after another.

If I execute this code:

channelOne = audio.play(sound1)
channelTwo = audio.play(sound2)

Both sounds will start playing at the same time.

The only way I was able to play one sounds after another is with the use of onComplete event.

So how do I do this? Do I have to chain the sounds using onComplete?

Thanks,
Mike
[import]uid: 24363 topic_id: 11528 reply_id: 311528[/import]

You can either do that or set the delay on each sound. I’ve found oncomplete chaining to be the easiest.

Depending what you’re doing, consider creating an array with the sounds … and the onComplete function just follows through that loop.

local sounds = { sound1, sound2, sound3 }  
  
local cnt = -1;  
local loopSounds = function ()  
 cnt = cnt + 1  
 if (#sounds \>= cnt) then  
 audio.play (sounds[cnt], { onComplete=loopSounds }   
 end  
  
end  
  
loopSounds ()  

… untested code, but I’m pretty sure that works. Probably better counter logic than starting with -1, but i can’t think of it right now and its beside the point. lol.

Hope this helps,
~~Kenn

[import]uid: 13859 topic_id: 11528 reply_id: 41848[/import]

you could store your sounds inside a table and play the next index of the table when a sound finished playing (yes, onComplete)… [import]uid: 69254 topic_id: 11528 reply_id: 41849[/import]

lol… it let me vote myself down. :wink: didn’t even realize that’s what those little things were there. :wink: [import]uid: 13859 topic_id: 11528 reply_id: 41850[/import]

local sounds = { sound1, sound2, sound3 }  
   
local cnt = 0;  
local loopSounds = function ()  
 cnt = cnt + 1  
 if (#sounds \>= cnt) then  
 audio.play (sounds[cnt], { onComplete=loopSounds }   
 end  
   
end  
   
audio.play (sounds[cnt], { onComplete=loopSounds }   

well that eliminates the -1 … though I’m not entirely sure it’s any better. :wink: [import]uid: 13859 topic_id: 11528 reply_id: 41851[/import]

Thank you both for the replies.
And thank you kennw for the code. [import]uid: 24363 topic_id: 11528 reply_id: 41914[/import]