Dispose audio in external lua, the proper way?

Hi,

I have a pop-up setting which will reload the page (using director), what is the proper way of dispose the audio and nil it when reloading page through external lua?

========

main.lua

========

director = require(“director”) 

director:changeScene ( “page1” )

==========

myaudio.lua

==========

local M = {}

  M.bgmusic = nil

  M.bgaudio = nil

return M

=======================================

loadsetting.lua (this load a button which when click, will reload page)

=======================================

local M = {}

local myaudio = require (“myaudio”)

M.new = function ( )    

    local function onbtn_closeEvent(event) 

    

        audio.stop(1); audio.stop(2);

            

        audio.dispose(myaudio.bgaudio);

        myaudio.bgaudio = nil

        

        audio.dispose(myaudio.bgmusic);

        myaudio.bgmusic = nil

        director:changeScene ( “page1” )

        

        return true

            

    end 

    local btn_close = display.newRect( 50, 50, 50, 50 )

    btn_close:addEventListener(“tap”, onbtn_closeEvent )

end

    

return M

=============

page1.lua

=============

module(…, package.seeall)

local myaudio = require (“myaudio”)

local mySetting = require (“loadsetting”)

function new()

    local menuGroup = display.newGroup()

    myaudio.bgmusic = audio.loadStream( “audio/bgm1.mp3”)

    myaudio.bgaudio = audio.loadStream( “audio/music1.mp3”)

    audio.play( myaudio.bgmusic, {  channel=1  } )

    audio.play( myaudio.bgaudio, {  channel=2  } )

    mySetting.new ()

    return menuGroup 

end 

Thanks.

Hi @jvmaster77,

Did you see this tutorial on handling cross-scene audio? I think it will help you considerably:

http://coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

Brent

@Brent, I rewrote my code after reading that handling cross scene audio tutorial, but I’m not sure if I’m doing it correctly this way, or, there is underlying issue with my method of handling it? Thanks.

Hi @jvmaster77,

Did you see this tutorial on handling cross-scene audio? I think it will help you considerably:

http://coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

Brent

@Brent, I rewrote my code after reading that handling cross scene audio tutorial, but I’m not sure if I’m doing it correctly this way, or, there is underlying issue with my method of handling it? Thanks.