Music stops when phone goes to sleep

Hi,

I am experiencing an issue when the iPhone (4S) goes into standby mode during running an application.

I have music playing (reserved channel 1). When the phone goes out of standby, the music does not resume, although all the other effects which are on unreserved channels still work.

Worse still, when an attempt is made to replay the music (on the same reserved channel), nothing happens.
You have to quit the app and restart.

The replay problem does not seem to happen if there is no music playing when the phone enters standby mode.

This problem is intermittent, so I’m wondering if there is a bug in Corona which prevents the music replaying, then prevents the channel being freed-up.

Any help would be really appreciated.

[import]uid: 87194 topic_id: 29770 reply_id: 329770[/import]

Forgot to mention that this is using the construct:

thisSound.channel = audio.play(thisSound.handle, { channel = 1, loops = 0, onComplete = musicComplete }); [import]uid: 87194 topic_id: 29770 reply_id: 119439[/import]

Hi, we are experiencing this as well. Have you found a solution? If not can any of the corona staff help us? [import]uid: 108204 topic_id: 29770 reply_id: 122369[/import]

I’m glad someone brought this up because I recall having the same problem as well. In my scenario, I use the following code:

local soundtrack = audio.loadStream("audio/soundtrack.mp3")  
audio.play( soundtrack, { channel=11, loops=-1 } )  

If I press the iPad Home button to exit my app, the music plays fine when I open the app again. However, if I press the iPad Hold button (the button on the top right-hand corner of the iPad) to exit the app, and then I enter the app again, my soundtrack is no longer playing although all my sound effects still work. [import]uid: 82194 topic_id: 29770 reply_id: 122389[/import]

After some trial and error, it seems that we managed to get audio working after the phone goes to sleep by manually pausing the audio on suspend, then resuming it on resume. It can be done like so:

function onSystemEvent(e)  
 local t = e.type  
  
 if t == "applicationSuspend" then  
 audio.pause()  
 elseif t == "applicationResume" then  
 audio.resume()  
 end  
 return true  
end  
  
Runtime:addEventListener("system", onSystemEvent)  

[import]uid: 108204 topic_id: 29770 reply_id: 122405[/import]

@TimeSpaceMagic - I believe that will fix the audio issue during suspend, but what about in the case where you press the Hold button (the button on the top right-hand corner of the iPad) to exit the app? For me, if I exit the app in this way, when I open the app again the soundtrack does not continue to play like it is supposed to, although the sound effects will still work. [import]uid: 82194 topic_id: 29770 reply_id: 122408[/import]

Hi , thanks for your comments.
I used a similar idea to David in the end - via trial and error!
stopMusicNow() is my own function to stop the audio.
startMusic() is my own function to start the audio again.

Unlike David’s solution, unfortunately mine starts the music file from scratch.

function onSystemEvent( event )  
 --print( "System event name and type: " .. event.name, event.type )  
 if (event.name == "system") then  
 if (event.type == "applicationResume") then  
 print("resume");  
 startMusic();  
  
 elseif (event.type == "applicationSuspend") then  
 sounds.stopMusicNow();  
  
 print("suspend");  
 end  
 end  
end  

Regards

[import]uid: 87194 topic_id: 29770 reply_id: 122790[/import]

@Nuimo, what’s David’s solution? Have you tried my solution? You don’t need to reload each audio from scratch.

@david, I assume you are unaware that the app does not exit on pressing the hold button? In fact, pressing the hold button (and the home button, or if the user receives a call) triggers the suspend state. Hence, my code will sufficiently cover your concerns. Did you try my code but fail to solve the problem? If so, do share your implementation and maybe I can help you out. [import]uid: 108204 topic_id: 29770 reply_id: 122807[/import]

@TimeSpaceMagic - I looked back at my code just now and discovered that on application suspend, I had been using audio.stop( soundtrack ) instead of audio.pause( ). Using audio.pause instead has now remedied my problem! You saved me there, thanks!! [import]uid: 82194 topic_id: 29770 reply_id: 122814[/import]

Hello TimeSpaceMagic,

In my reply I meant to refer to your solution not David’s! Sorry to confuse (just in process of trying to submit my first game so my stress-brain-centres are working overtime :wink:

I would love to test your solution - will go into the next update of the game!

[import]uid: 87194 topic_id: 29770 reply_id: 122835[/import]

Hi, we are experiencing this as well. Have you found a solution? If not can any of the corona staff help us? [import]uid: 108204 topic_id: 29770 reply_id: 122369[/import]

I’m glad someone brought this up because I recall having the same problem as well. In my scenario, I use the following code:

local soundtrack = audio.loadStream("audio/soundtrack.mp3")  
audio.play( soundtrack, { channel=11, loops=-1 } )  

If I press the iPad Home button to exit my app, the music plays fine when I open the app again. However, if I press the iPad Hold button (the button on the top right-hand corner of the iPad) to exit the app, and then I enter the app again, my soundtrack is no longer playing although all my sound effects still work. [import]uid: 82194 topic_id: 29770 reply_id: 122389[/import]

After some trial and error, it seems that we managed to get audio working after the phone goes to sleep by manually pausing the audio on suspend, then resuming it on resume. It can be done like so:

function onSystemEvent(e)  
 local t = e.type  
  
 if t == "applicationSuspend" then  
 audio.pause()  
 elseif t == "applicationResume" then  
 audio.resume()  
 end  
 return true  
end  
  
Runtime:addEventListener("system", onSystemEvent)  

[import]uid: 108204 topic_id: 29770 reply_id: 122405[/import]

@TimeSpaceMagic - I believe that will fix the audio issue during suspend, but what about in the case where you press the Hold button (the button on the top right-hand corner of the iPad) to exit the app? For me, if I exit the app in this way, when I open the app again the soundtrack does not continue to play like it is supposed to, although the sound effects will still work. [import]uid: 82194 topic_id: 29770 reply_id: 122408[/import]

Hi , thanks for your comments.
I used a similar idea to David in the end - via trial and error!
stopMusicNow() is my own function to stop the audio.
startMusic() is my own function to start the audio again.

Unlike David’s solution, unfortunately mine starts the music file from scratch.

function onSystemEvent( event )  
 --print( "System event name and type: " .. event.name, event.type )  
 if (event.name == "system") then  
 if (event.type == "applicationResume") then  
 print("resume");  
 startMusic();  
  
 elseif (event.type == "applicationSuspend") then  
 sounds.stopMusicNow();  
  
 print("suspend");  
 end  
 end  
end  

Regards

[import]uid: 87194 topic_id: 29770 reply_id: 122790[/import]

@Nuimo, what’s David’s solution? Have you tried my solution? You don’t need to reload each audio from scratch.

@david, I assume you are unaware that the app does not exit on pressing the hold button? In fact, pressing the hold button (and the home button, or if the user receives a call) triggers the suspend state. Hence, my code will sufficiently cover your concerns. Did you try my code but fail to solve the problem? If so, do share your implementation and maybe I can help you out. [import]uid: 108204 topic_id: 29770 reply_id: 122807[/import]

@TimeSpaceMagic - I looked back at my code just now and discovered that on application suspend, I had been using audio.stop( soundtrack ) instead of audio.pause( ). Using audio.pause instead has now remedied my problem! You saved me there, thanks!! [import]uid: 82194 topic_id: 29770 reply_id: 122814[/import]

Hello TimeSpaceMagic,

In my reply I meant to refer to your solution not David’s! Sorry to confuse (just in process of trying to submit my first game so my stress-brain-centres are working overtime :wink:

I would love to test your solution - will go into the next update of the game!

[import]uid: 87194 topic_id: 29770 reply_id: 122835[/import]