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? [import]uid: 85633 topic_id: 14630 reply_id: 314630[/import]

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

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 14630 reply_id: 54115[/import]

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:

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

When it switches to a new scene, the music does NOT stop, it continues to play. Then, on my back button graphic, I have it written to stop the music playing like this:

 local buttonHome = display.newImage("img\_backButton.png")  
 function buttonHome:touch(event)  
 if (event.phase == "began") then   
 audio.fade(soundtrack) -- fades out soundtrack here!  
 elseif (event.phase == "ended") then  
 print("Home, ended phase")  
 timer.performWithDelay(1000, function() director:changeScene("screen1","moveFromLeft") end, 1)  
 end  

So basically what is going on here is that the music starts on the menu scene, and on the second scene my back button first fades out the music, performs a short delay to let the music fade completely away, then switches back to the menu screen where the music then starts up again.

I tested this on an Android device (HTC Desire S) and on my iPhone and encountered a separate issue that I was hoping you could test out as well. On my Android device, if the music is still playing and I press my device’s home button the music won’t stop and will continue to play for about 4 seconds before it stops. Then when I restart the app the music sounds like it’s having a hard time playing. But with my iPhone, I have no problem at all and when I click on my iPhone device’s home button the music stops just perfectly.

Are you able to test this issue as well and let me know if you’re having the same problem? Because if yes, then I’m thinking of just not including music on my Android version but keeping the music on my iPhone version.

[import]uid: 82194 topic_id: 14630 reply_id: 54118[/import]

Well, the problem isn’t that it’s stopping, it’s that it won’t start again upon returning to the scene. [import]uid: 85633 topic_id: 14630 reply_id: 54122[/import]

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. [import]uid: 85633 topic_id: 14630 reply_id: 54123[/import]

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 :frowning:

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. [import]uid: 85633 topic_id: 14630 reply_id: 54130[/import]

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. [import]uid: 84637 topic_id: 14630 reply_id: 54226[/import]