Audio.dispose in Storyboard

I am trying to create an ebook. I want a certain sound to play in scene 2 and when i click on the nextPage button I have i want to get rid of that audio so that it no longer plays in scene 3. Where do I need to put audio.dispose and do I need to place audio.stop in my code before I dispose of the sound? I tried to put audio.dispose in the nextPg:tap(e) function below but when I tapped the nextPage button it just caused the audio to “skip” in scene 3. Any help would be greatly appreciated, thank you!
local storyboard = require(“storyboard”);
local scene = storyboard.newScene()

mySong = audio.play(backgroundSound, {channel=1, loops=-1, fadein=3000} )

local nextPg;
function scene:createScene(e)

local view = self.view

local background = display.newImage( “cover2.png” );

nextPg = display.newImage( “nextPage.png” );
nextPg.x = 949
nextPg.y = 53

function nextPg:tap(e)

storyboard.gotoScene(“scene3”,{
effect = “slideLeft”, – transition effect to implement
time = 250 – duration of the transition effect in milliseconds
});

end

nextPg:addEventListener(“tap”,nextPg);

view:insert(background);
view:insert(nextPg);

end

function scene:enterScene(e)

end

function scene:exitScene(e)
storyboard.purgeScene(“scene2”);
end

scene:addEventListener(“createScene”, scene);
scene:addEventListener(“enterScene”, scene);
scene:addEventListener(“exitScene”, scene);
return scene [import]uid: 146168 topic_id: 30620 reply_id: 330620[/import]

I just ran into a similar issue and a recent blog post on how modules work enlightened me.

If you do your

somesound = audio.load(“mysounds.mp3”)

at the top of your storyboard file, that is, it’s not inside of a createScene or enterScene function, then it gets loaded the first time you hit that scene. Even if you purge or remove the scene later using storyboards .removeScene or .purgeScene methods, that module is still loaded and that code will never execute again if you return to that scene… unless you go through the complete module removal method.

The takeway from this is that you should be loading your sounds during the createScene phase if you plan on re-entering that scene later.

Yea, yea, but what about disposing…

My next problem came to me when I tried to put the dispose calls in destroyScene which gets called after the next scene is loaded in my case (I fire off a timer to do a removeAll a second later). That actually works pretty well. Create them in createScene and destroy them in destroyScene, but wait…

The audio has to be stopped before it can be disposed and I have some long voice overs that can carry over a bit into the next scene. Easy enough audio.stop(channelNo) but here is where the fun starts, the next scene starts playing its sound on that channel (I do a stop before I start the next voice over).

So I had to make sure in exitScene() I stop the audio for that scene, the next scene can do its thing, then I let destroyScene actually call the dispose code.

The gist of all of this:

create sounds in createScene()
play them in enterScene()
stop them in exitScene()
dispose them in destroyScene()
[import]uid: 19626 topic_id: 30620 reply_id: 122682[/import]

That works great, thank you for your help! [import]uid: 146168 topic_id: 30620 reply_id: 122690[/import]

How would I do the same process with a sprite?

Bring in the sprite, start the sprite in scene 2 and stop the sprite when going to scene 3, would I then put ( in my case) instance1:removeSelf() in the destroyScene function? [import]uid: 146168 topic_id: 30620 reply_id: 122693[/import]

I just ran into a similar issue and a recent blog post on how modules work enlightened me.

If you do your

somesound = audio.load(“mysounds.mp3”)

at the top of your storyboard file, that is, it’s not inside of a createScene or enterScene function, then it gets loaded the first time you hit that scene. Even if you purge or remove the scene later using storyboards .removeScene or .purgeScene methods, that module is still loaded and that code will never execute again if you return to that scene… unless you go through the complete module removal method.

The takeway from this is that you should be loading your sounds during the createScene phase if you plan on re-entering that scene later.

Yea, yea, but what about disposing…

My next problem came to me when I tried to put the dispose calls in destroyScene which gets called after the next scene is loaded in my case (I fire off a timer to do a removeAll a second later). That actually works pretty well. Create them in createScene and destroy them in destroyScene, but wait…

The audio has to be stopped before it can be disposed and I have some long voice overs that can carry over a bit into the next scene. Easy enough audio.stop(channelNo) but here is where the fun starts, the next scene starts playing its sound on that channel (I do a stop before I start the next voice over).

So I had to make sure in exitScene() I stop the audio for that scene, the next scene can do its thing, then I let destroyScene actually call the dispose code.

The gist of all of this:

create sounds in createScene()
play them in enterScene()
stop them in exitScene()
dispose them in destroyScene()
[import]uid: 19626 topic_id: 30620 reply_id: 122682[/import]

That works great, thank you for your help! [import]uid: 146168 topic_id: 30620 reply_id: 122690[/import]

How would I do the same process with a sprite?

Bring in the sprite, start the sprite in scene 2 and stop the sprite when going to scene 3, would I then put ( in my case) instance1:removeSelf() in the destroyScene function? [import]uid: 146168 topic_id: 30620 reply_id: 122693[/import]