is it possible to pass a table listener to audio.play?

Hi I’m trying to chain a number of sounds together in an app coded in Object orientation style.

I want to be able to play sounds sequentially using the onComplete handler and couldnt see how to use a table listener

something like this

[lua]–doesnt work :frowning:
function myclass:playit()
self.nextsound=oNextSound
audio.play(self.oSound, {onComplete=self})
end

function myclass:onComplete()
audio.dispose(self.oSound)
audio.play(self.nextsound, {onComplete=self})
end[/lua]

I could do it procedurally, but prefer to stay in OO mode.
is there a workaround that I could use?

Thanks [import]uid: 74338 topic_id: 20594 reply_id: 320594[/import]

Maybe you can use closures?

http://blog.anscamobile.com/2011/02/using-closures-in-lua-to-avoid-global-variables-for-callbacks/

– Not sure if the following works…just thinking out loud
function myclass:playit()
self.nextsound=oNextSound
audio.play(self.oSound, {onComplete=function() self:onComplete() end})
end

function myclass:onComplete()
audio.dispose(self.oSound)
audio.play(self.nextsound, {onComplete=?})
end
[import]uid: 7563 topic_id: 20594 reply_id: 80894[/import]

perfect :slight_smile: worked a treat [import]uid: 74338 topic_id: 20594 reply_id: 80939[/import]

finished code

[lua]–*******************************************************
function cNaggerSounds:getHandle(psFolder, paSounds)
local i, sPath, sFile

–choose a random direction
i = math.random( table.maxn(paSounds))
sFile = paSounds[i]
sPath = psFolder…"/"…sFile

–play the file
return audio.loadSound(sPath)
end

–*******************************************************
function cNaggerSounds:playRandom()
local i

– mutex
if self.playing then return end
self.playing = true

– load the sound
self.handle = self:getHandle(self.data.folders.directions, self.data.directions)

– use closures to pass parameters and play
fnCallback = function(poEvent) self:onComplete1(poEvent) end
audio.play(self.handle, {onComplete=fnCallback})
end

–*******************************************************
function cNaggerSounds:onComplete1(poEvent)
local i, sPath, sFolder, sFile, oHandle, fnCallback

– clear out the previous sound
audio.dispose(self.handle)
self.handle = nil

– load the sound
self.handle = self:getHandle(self.data.folders.nags, self.data.nags)

– use closures to pass parameters
fnCallback = function(poEvent) self:onComplete2(poEvent) end
audio.play(self.handle, {onComplete=fnCallback})
end

–*******************************************************
function cNaggerSounds:onComplete2(poEvent)
audio.dispose(self.handle)
self.handle = nil
self.playing = false
end[/lua] [import]uid: 74338 topic_id: 20594 reply_id: 80941[/import]