Help!How to play Background Music Audio along the Scenes(Storyboard API)

Hi All,

I would like to play Game’s Background Music audio along the Scenes(Storyboard) without stop and start playing again (Continuously play the Background music).

Let say I have three Scene.I need to play a background music across the Scenes.I don’t want to stop the audio when exit the Scene and start playing again in another Scene.

May I know how to continuously play the Background music along the Scenes.

Best Regards,

John

You could put something like this into main.lua

​audio.loadStream("backgroundMusic.mp3") storyboard.startBGM = function() --set loops = -1 to loop infinitely local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1} ) end

Then wherever you want the background music to start, call 

storyboard.startBGM()

It will then loop indefinitely, until you stop it. Attaching it to the storyboard object also means that you can start it from any storyboard scene.

Hi John,

This tutorial might also help you… it deals with using cross-scene audio in a module:

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

Brent

You could put something like this into main.lua

​audio.loadStream("backgroundMusic.mp3") storyboard.startBGM = function() --set loops = -1 to loop infinitely local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1} ) end

Then wherever you want the background music to start, call 

storyboard.startBGM()

It will then loop indefinitely, until you stop it. Attaching it to the storyboard object also means that you can start it from any storyboard scene.

Hi John,

This tutorial might also help you… it deals with using cross-scene audio in a module:

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

Brent

Didn’t work for me. I am using:

defaultTransition = “crossFade” – the effect to use when switching scenes
 

in main.lua to dissolve between scenes. Maybe this is why?

Still fades out the audio on scene change.

I bet you want a splash screen + a starting background music at once?

-Main.lua display.setStatusBar( display.HiddenStatusBar ) -- Hide the ugly status bar local storyboard = require ("storyboard") display.setDefault( "background", 0, 0, 0 ) -- Sets the background to  black. local intro = display.newImage("splash.jpg", true ) -- the splash screen intro.x = display.contentWidth\*0.5 intro.y = display.contentHeight\*0.5 menu\_music = audio.loadStream("menu\_music.ogg") --you can compress ogg better without quality loss most of the time function playMusic()     audio.play(menu\_music ,{ channel=1, loops=-1, fadein=5000}) --fadein means that it starts at low volume getting higher end function switchscene()  storyboard.gotoScene( "menu" ) end transition.to(intro, {time = 0, delay = 0, alpha = 0.0, onComplete=playMusic}) transition.to(intro, {time = 2000, delay = 1000, alpha = 1.0}) transition.to(intro, {time = 2500, delay = 3000, alpha = 0.0}) transition.to(intro, {time = 300, delay = 5500, alpha = 0.0, onComplete=switchscene})  

Didn’t work for me. I am using:

defaultTransition = “crossFade” – the effect to use when switching scenes
 

in main.lua to dissolve between scenes. Maybe this is why?

Still fades out the audio on scene change.

I bet you want a splash screen + a starting background music at once?

-Main.lua display.setStatusBar( display.HiddenStatusBar ) -- Hide the ugly status bar local storyboard = require ("storyboard") display.setDefault( "background", 0, 0, 0 ) -- Sets the background to  black. local intro = display.newImage("splash.jpg", true ) -- the splash screen intro.x = display.contentWidth\*0.5 intro.y = display.contentHeight\*0.5 menu\_music = audio.loadStream("menu\_music.ogg") --you can compress ogg better without quality loss most of the time function playMusic()     audio.play(menu\_music ,{ channel=1, loops=-1, fadein=5000}) --fadein means that it starts at low volume getting higher end function switchscene()  storyboard.gotoScene( "menu" ) end transition.to(intro, {time = 0, delay = 0, alpha = 0.0, onComplete=playMusic}) transition.to(intro, {time = 2000, delay = 1000, alpha = 1.0}) transition.to(intro, {time = 2500, delay = 3000, alpha = 0.0}) transition.to(intro, {time = 300, delay = 5500, alpha = 0.0, onComplete=switchscene})