Streaming background music stops and won't restart on scene change

I am using the Director Class to change between scenes.

On my main menu, I can load up background music, and it plays fine as follows:

local backgroundSound = audio.loadStream("song.mp3")
audio.play(backgroundSound,{channel=1,loops=-1,fadein=1000})

Now, I change scenes to another screen, the music stops, it stops and disposes of the audio in the cleanup function.

On the next screen, I can hit a back button that changes the scene again back to the main menu. HOWEVER, upon returning to the mainmenu.lua function, the music won't start playing again.

Does anyone know what may be happening?

If you add the song to the localGroup it will stop playing when you change scenes.

Regards,
Jordan Schuetz
Ninja Pig Studios

In your code, instead of:
audio.play(backgroundSound,{channel=1,loops=-1,fadein=1000}).

try:
audio.play(backgroundSound,{loops=-1,fadein=1000}

Just curious if that works? I have a question for you, because I encountered your exact issue and here is how I went about resolving it.

Using Director, and on my menu page, I had it coded this way:

1
2
local soundtrack = audio.loadStream("audio_soundtrack.wav")
audio.play(soundtrack,{loops=-1, fadein=100})

Well, the problem isn't that it's stopping, it's that it won't start again upon returning to the scene.

Thanks, I'll try removing the channel and see if that helps.

Lots of games on my iPhone play music for several seconds after the app closes, but I'll let you know if I experience the same.

OK, so I figured it out.

In my cleanupAudio function in each scene, I do an "audio.stop()"

So, when I would press the Back button from one of my other screens, it would try to start the music in my mainmenu scene, but the cleanup of the previous scene appears to run AFTER the scene changes and starts setting itself up again. So, the cleanup STOPS the audio that just tried to START :(

My final solution was to start the music after a 1.5 second delay when the main menu screen comes up - allowing plenty of time for the cleanup on the previous scene to finish up.

I put this at the END of my mainmenu scene new() function:

local startMusic = function()
audio.play( backgroundSound, {loops=-1, fadein=1000 })
timer.cancel ( theSoundTimer );
theSoundTimer=nil;
end

theSoundTimer = timer.performWithDelay(1500, startMusic, 1)

I'm not sure if this is the correct way to do this, so any input would be appreciated, but this IS working for me right now as far as I can tell.

There is nothing coherently wrong with using a delay to start your audio.

If you don't want to do it that way, maybe take your audio disposal code out of the cleanUp function and manually manage it upon pressing the button to change scenes.

views:1611 update:2011/10/4 17:12:07
corona forums © 2003-2011