audio.play ( onComplete ... audio.play ) crashing on leaving scene

So I have this piece of code:

 audio.play( jumpSound, { channel=2, onComplete=function() audio.play( mySounds[item.name] ) end } )

The app crashes if I leave the scene while the outer audio.play is still playing. How can I overcome this problem?

Thanks.

Hi @jongar,

By leaving the scene, do you clear/nil the reference to “mySounds” and/or “item.name”? Meaning, does that reference go out of scope, such that the audio engine doesn’t know what you’re asking it to play?

Brent

Yes, I am doing this scene:hide even phase will:

 for i=1, #myTable do audio.dispose( tapTheSounds[myTable[i].name] ) tapTheSounds[myTable[i].name] = nil end

Hi @jongar,

How are you controlling audio channels in general? I see that you’re using channel 2 for “jumpSound”, while letting the audio engine find a free channel for the others.

The reason I ask is, can you stop all sounds when the scene will hide? Or do you want some sounds to continue playing? The latter is a little more tricky, but possible.

In any case, you should attempt to stop all sounds before you dispose() of the loaded handle.

Also, you might want to try looping backwards through the table when you dispose of the audio handles. This is because of how Lua iterates over a table while clearing items… if you do it forward (default) it can actually result in skipping every other instance. So, I would suggest this:

[lua]

for i=#myTable,1,-1 do

    audio.dispose( tapTheSounds[myTable[i].name] )

    tapTheSounds[myTable[i].name] = nil

end

[/lua]

Brent

Hi @jongar,

By leaving the scene, do you clear/nil the reference to “mySounds” and/or “item.name”? Meaning, does that reference go out of scope, such that the audio engine doesn’t know what you’re asking it to play?

Brent

Yes, I am doing this scene:hide even phase will:

 for i=1, #myTable do audio.dispose( tapTheSounds[myTable[i].name] ) tapTheSounds[myTable[i].name] = nil end

Hi @jongar,

How are you controlling audio channels in general? I see that you’re using channel 2 for “jumpSound”, while letting the audio engine find a free channel for the others.

The reason I ask is, can you stop all sounds when the scene will hide? Or do you want some sounds to continue playing? The latter is a little more tricky, but possible.

In any case, you should attempt to stop all sounds before you dispose() of the loaded handle.

Also, you might want to try looping backwards through the table when you dispose of the audio handles. This is because of how Lua iterates over a table while clearing items… if you do it forward (default) it can actually result in skipping every other instance. So, I would suggest this:

[lua]

for i=#myTable,1,-1 do

    audio.dispose( tapTheSounds[myTable[i].name] )

    tapTheSounds[myTable[i].name] = nil

end

[/lua]

Brent