Hi all,
Thank you for your help earlier this week regarding the basics of using scene changed in composer - I have one final question that I’m hoping someone can help me with.
Basically, I have some music that plays when you go to a scene, however, when you go to another scene, the music carries on without stopping, I think it has something to do with the way you destroy the audio on the scene change, however, my method (below) unfortunately doesnt seem to work, does anyone know what the correct code should be?
Thanks again for your help, I really appreciate it.
Chris
local composer = require( “composer” )
local scene = composer.newScene()
– Code outside of the scene event functions below will only be executed ONCE unless
– the scene is removed entirely (not recycled) via “composer.removeScene()”
– Scene event functions
local bg
local title
local button
local function changeScenes ()
composer.gotoScene(“homepage”, {effect = “crossFade”, time = 500})
end
– create()
function scene:create( event )
local function narrationFinished( event )
print( “Narration Finished on channel”, event.channel )
if ( event.completed ) then
print( “Narration completed playback naturally” )
else
print( “Narration was stopped before completion” )
end
end
– Load two audio streams and one sound
local backgroundMusic = audio.loadStream( “backgroundMusic.m4a” )
local narrationSpeech = audio.loadStream( “narrationSpeech.wav” )
local laserSound = audio.loadSound( “laserBlast.wav” )
– Play the background music on channel 1, loop infinitely, and fade in over 5 seconds
local backgroundMusicChannel = audio.play( backgroundMusic, { channel=4, loops=-1, fadein=5000 } )
– Play the speech on any available channel, for 30 seconds at most, and invoke listener function when audio finishes
local narrationChannel = audio.play( narrationSpeech, { duration=30000, onComplete=narrationFinished } )
– Play the laser on any available channel
local laserChannel = audio.play( laserSound )
local sceneGroup = self.view
– Code here runs when the scene is first created but has not yet appeared on screen
bg = display.newRect(display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight)
bg:setFillColor(0,0,1)
sceneGroup: insert(bg)
title = display.newText(“PORSCHE”, display.contentCenterX, display.contentCenterY, “Arial”, 60)
title:setFillColor(1,1,1)
sceneGroup:insert(title)
button = display.newRect(display.contentCenterX, display.contentHeight*.9, display.contentWidth*.2, display.contentHeight*.05)
button:setFillColor(1,0,0)
sceneGroup:insert(button)
button:addEventListener(“tap”, changeScenes)
– Code here runs when the scene is first created but has not yet appeared on screen
end
– show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
end
end
– hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == “did” ) then
– Code here runs immediately after the scene goes entirely off screen
end
end
– destroy()
function scene:destroy( event )
local sceneGroup = self.view
local backgroundMusic = audio.loadStream
– Code here runs prior to the removal of scene’s view
end
– Scene event function listeners
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene