Don't stop background music

Hi, It looks like the default behavior of a corona app is that it will stop whatever music is coming from the music player when the app loads up. Is there a way to prevent that from happening? I would rather users be able to play their own music however they want to.

I thought I saw a post where the answer was yes a while back but both google/forum search didn’t help and I can’t find anything in the api documentation(though if it’s kind of easy to miss things there!). [import]uid: 147305 topic_id: 30438 reply_id: 330438[/import]

Yes, on iOS - see the last post by ewing here: http://developer.coronalabs.com/forum/2012/06/03/playing-music-background-android

Peach :slight_smile: [import]uid: 52491 topic_id: 30438 reply_id: 121947[/import]

Thanks, but that thread actually addresses having your app play it’s sounds/music while it is suspended. i am looking for my app to not interrupt/stop music playing from a separate app(such as whatever native music player the device has) when my app is launched/comes into focus.

Here is what happens currently:

  1. I’m jamming to so MC Hammer that is playing in the iPhone’s music player.
  2. I decide to play one of my apps while listening to some sweet funky jams.
  3. I open up my app
  4. MC Hammer stops playing when my app gets focus.
  5. I get depressed because I can’t listen to the epic ballad of “You can’t touch this” while using my app.

What i want to prevent is step 4 and 5. [import]uid: 147305 topic_id: 30438 reply_id: 121991[/import]

I just asked Eric this exact thing the other day and still have his email reply;

“Yes. They need the advanced Audio Session APIs to set the appropriate audio session category to allow mixing with the iPod music.”

That’s about the user playing their own music in the background - if you call MC Hammer music :wink: [import]uid: 52491 topic_id: 30438 reply_id: 122096[/import]

Well I’m not sure where the advanced audio session api stuff is but I’m not that worried about. I was hoping for a simple flag. Being that it looks more involved and isn’t super important I’ll just let it go. Thanks! [import]uid: 147305 topic_id: 30438 reply_id: 122172[/import]

I got this to work sort of, music suspends when app is launched, but the music can be manually restarted after the app launches/ It sure a lot better than nothing. My game is a card game and I can still easily hear the game beeps/sounds with the music playing.

--------code to allow Native music to play in background!!!!  
--I put this towards the top of main.lua  
--When app lauches, the music still fades out but can atleast be restarted and played while in app  
  
-- If another app/iPod is playing background music, then allow that music to continue playing,  
-- otherwise, play my own background music  
if audio.getSessionProperty( audio.OtherAudioIsPlaying ) == 1 then   
 audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)  
  
end  
   
 --ME...not really sure what the code below does, maybe preserves vol, maybe nothing, but I left it  
-- Backdoor trick (we will not support this):   
-- If you do some research, you can learn that Apple's constant for   
-- kAudioSessionProperty\_CurrentHardwareOutputVolume  
-- is 1667788662. As an implementation detail, our API currently will pass numbers through.  
-- Note kAudioSessionProperty\_CurrentHardwareOutputVolume is a read-only property.  
local master\_ringer\_volume = audio.getSessionProperty(1667788662)  

Hope this helps.

Nail [import]uid: 106779 topic_id: 30438 reply_id: 122178[/import]

Yes, on iOS - see the last post by ewing here: http://developer.coronalabs.com/forum/2012/06/03/playing-music-background-android

Peach :slight_smile: [import]uid: 52491 topic_id: 30438 reply_id: 121947[/import]

Thanks, but that thread actually addresses having your app play it’s sounds/music while it is suspended. i am looking for my app to not interrupt/stop music playing from a separate app(such as whatever native music player the device has) when my app is launched/comes into focus.

Here is what happens currently:

  1. I’m jamming to so MC Hammer that is playing in the iPhone’s music player.
  2. I decide to play one of my apps while listening to some sweet funky jams.
  3. I open up my app
  4. MC Hammer stops playing when my app gets focus.
  5. I get depressed because I can’t listen to the epic ballad of “You can’t touch this” while using my app.

What i want to prevent is step 4 and 5. [import]uid: 147305 topic_id: 30438 reply_id: 121991[/import]

I just asked Eric this exact thing the other day and still have his email reply;

“Yes. They need the advanced Audio Session APIs to set the appropriate audio session category to allow mixing with the iPod music.”

That’s about the user playing their own music in the background - if you call MC Hammer music :wink: [import]uid: 52491 topic_id: 30438 reply_id: 122096[/import]

Well I’m not sure where the advanced audio session api stuff is but I’m not that worried about. I was hoping for a simple flag. Being that it looks more involved and isn’t super important I’ll just let it go. Thanks! [import]uid: 147305 topic_id: 30438 reply_id: 122172[/import]

I got this to work sort of, music suspends when app is launched, but the music can be manually restarted after the app launches/ It sure a lot better than nothing. My game is a card game and I can still easily hear the game beeps/sounds with the music playing.

--------code to allow Native music to play in background!!!!  
--I put this towards the top of main.lua  
--When app lauches, the music still fades out but can atleast be restarted and played while in app  
  
-- If another app/iPod is playing background music, then allow that music to continue playing,  
-- otherwise, play my own background music  
if audio.getSessionProperty( audio.OtherAudioIsPlaying ) == 1 then   
 audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)  
  
end  
   
 --ME...not really sure what the code below does, maybe preserves vol, maybe nothing, but I left it  
-- Backdoor trick (we will not support this):   
-- If you do some research, you can learn that Apple's constant for   
-- kAudioSessionProperty\_CurrentHardwareOutputVolume  
-- is 1667788662. As an implementation detail, our API currently will pass numbers through.  
-- Note kAudioSessionProperty\_CurrentHardwareOutputVolume is a read-only property.  
local master\_ringer\_volume = audio.getSessionProperty(1667788662)  

Hope this helps.

Nail [import]uid: 106779 topic_id: 30438 reply_id: 122178[/import]

audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)

put that at the top of main.lua

works for me on iOS, I guess it needs to be at top of main.lua so it is called straight away before corona gets a chance to kill your music.

audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)

put that at the top of main.lua

works for me on iOS, I guess it needs to be at top of main.lua so it is called straight away before corona gets a chance to kill your music.