[Resolved] Question about audio.dispose()

I am having an issue when calling audio.dispose() to clear some memory. Basically, I create an audio with my background music in it, call it immediately to start playing, and then want to dispose of it when I change scenes. (I am using Director BTW) Everything worked fine with the below code except when i add in audio.dispose(bg_music); I get a crash. Heres what I’m working with.

[lua]local bg_music = audio.loadStream(“sounds/background.wav”)
audio.play(bg_music);

–Body of code here doing stuff while bg_music plays
–…
–…
local function changeScene(e)
audio.stop(bg_music);
audio.dispose(bg_music);
if(e.phase == “ended” or e.phase == “cancelled”) then
director:changeScene(e.target.scene);
end
end

buttonToChangeScene:addEventListener(“touch”, changeScene);[/lua]

If “audio.dispose(bg_music);” is commented out, the code works fine. I should also mention I use the same background music for each level so when the scene is changed I would like the bg_music to stop, then be unloaded. This was I can load it again in the next level and not have multiple copies using memory. (I know thats not the best way to explain it but its getting late haha) Thanks again in advance, I could’t even imagine being this far into my app development without this forums help!

[import]uid: 50511 topic_id: 25857 reply_id: 325857[/import]

This is because you are calling it twice.

You have a touch listener and audio.dispose is being called on all event phases as you haven’t specified - put it inside the ended phase and it should do the trick; or remove the phase if statement altogether and just use “tap” rather than “touch”.

Peach :slight_smile: [import]uid: 52491 topic_id: 25857 reply_id: 104610[/import]

Oh geez. I must be putting in too many hours haha I could not figure that that out for the life of me. Thanks again Peach! [import]uid: 50511 topic_id: 25857 reply_id: 104665[/import]

No worries, it’s easy to do when using touch listeners. Marking as resolved :slight_smile: [import]uid: 52491 topic_id: 25857 reply_id: 104822[/import]