When I open my application on my device any music that was previously going is immediately cut out. Is there an option somewhere that allows the music from a separate app to continue going in the background when opening and playing one of my Corona-made applications?
Hi Corey, have you tried adding the code below to main.lua?
Naomi
[lua]
– allow other audio to continue to play
– i.e., if the user is already listening to music before launching the game, it will continue to play.
– Edit: Ummm, I thought the below code didn’t work, but actually it does work after all.
if audio.supportsSessionProperty == true then
print(“supportsSessionProperty is true”)
audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)
if not(audio.getSessionProperty(audio.OtherAudioIsPlaying) == 0) then
print(“I think there is other Audio Playing”)
end
end
[/lua]
Edit: BTW, for the life of me, I don’t remember and can’t find where I found these code snippet. I thought I might find it in API doc page for audio, but I didn’t. I’ve never really tested if this code is actually doing what it’s supposed to do.
So, Corey, will you post back and let me know if it’s working or not? I’d appreciate it. Thanks.
On iOS it’s possible to do this using some unofficial audio APIs. (Even though they’re unofficial, they’ve worked for me for over a year.)
Here’s a thread that discusses it: http://forums.coronalabs.com/topic/8742-new-audiosession-properties/page-3#entry144436.
- Andrew
Ah, Andrew @aukStudio, good to know it works only for iOS. I’ll make a note of it.
Thanks!
Naomi
Thanks for the quick replies!
I tried out what aukStudios said and it works like a charm!
This is what i added to my main.lua file-
if audio.supportsSessionProperty == true then audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode) end audio.OtherAudioIsPlaying later gives misleading results, since at that point the app itself may be playing audio local isOtherAudioPlaying = false if audio.supportsSessionProperty == true then if not(audio.getSessionProperty(audio.OtherAudioIsPlaying) == 0) then isOtherAudioPlaying = true player.music = false end end
Thanks,
-Corey Simpson
Ah, thanks, Corey. I didn’t look at Andrew’s linked post. It sounds like I need to update my code.
Cheers,
Naomi
Hey, Andrew @aukStudios, about your post in the other thread (http://forums.coronalabs.com/topic/8742-new-audiosession-properties/page-3#entry144436), I have a question regarding the on suspend and resume events.
I assume I need the runtime system event listener in main.lua, and on “applicationSuspend”, I’d call audio.stop, and on “applicationResume”, I’d check the flag for other audio, and if other audio is playing, I won’t play the music. Does this cover the basic flow? (My theme music is loaded globally and I don’t intend to dispose and re-load upon suspend/resume events.)
Naomi
Hey Naomi,
Yep, that’s the right flow.
My recollection is that, on suspend events, it was also necessary to actually dispose of the audio via audio.dispose() after stopping it via audio.stop(). Merely calling audio.stop() wasn’t sufficient to ensure that, when the application resumed, audio.OtherAudioIsPlaying would give the right result (i.e., correctly indicate whether iTunes is playing any music). But I haven’t tested it for a long time, so it’s possible that audio.dispose() may not have been necessary after all. I’d be curious to know if that’s what you find.
- Andrew
Thank you, Andrew! I’m wondering if you dispose all sound effects when you dispose music.
The thing is, if you don’t dispose the sound effects, then I imagine it’s okay to keep the music loaded in memory too… The way I do it, I have all required sound effects and music loaded in memory and play them when required. So, if it’s okay to keep the sound effects in memory for the flag (OtherAudioIsPlaying) to work correctly, then I’ll keep the music loaded too. However, if you dispose of all loaded sound effects and music, and then load them again upon applicationResume, then I suppose that’s how it needs to be done…
I do not wish to dispose all sound files and then re-load them upon applicationResume – so if it needs to be so, I may do away with runtime event listener bit. My settings menu allows user to have music & sound effects or sound effects only or none. So if a user wants to play their own music, they could just turn off the music from my game while keeping the sound effects after all…
Naomi
Hi Naomi,
No, I don’t dispose of all my sound effects when the app suspends. I only dispose the music. And even that may not actually be necessary (I thought it was at the time, but I haven’t tested it since then).
- Andrew
Got it. Thank you, Andrew!
Naomi
Hi Corey, have you tried adding the code below to main.lua?
Naomi
[lua]
– allow other audio to continue to play
– i.e., if the user is already listening to music before launching the game, it will continue to play.
– Edit: Ummm, I thought the below code didn’t work, but actually it does work after all.
if audio.supportsSessionProperty == true then
print(“supportsSessionProperty is true”)
audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)
if not(audio.getSessionProperty(audio.OtherAudioIsPlaying) == 0) then
print(“I think there is other Audio Playing”)
end
end
[/lua]
Edit: BTW, for the life of me, I don’t remember and can’t find where I found these code snippet. I thought I might find it in API doc page for audio, but I didn’t. I’ve never really tested if this code is actually doing what it’s supposed to do.
So, Corey, will you post back and let me know if it’s working or not? I’d appreciate it. Thanks.
On iOS it’s possible to do this using some unofficial audio APIs. (Even though they’re unofficial, they’ve worked for me for over a year.)
Here’s a thread that discusses it: http://forums.coronalabs.com/topic/8742-new-audiosession-properties/page-3#entry144436.
- Andrew
Ah, Andrew @aukStudio, good to know it works only for iOS. I’ll make a note of it.
Thanks!
Naomi
Thanks for the quick replies!
I tried out what aukStudios said and it works like a charm!
This is what i added to my main.lua file-
if audio.supportsSessionProperty == true then audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode) end audio.OtherAudioIsPlaying later gives misleading results, since at that point the app itself may be playing audio local isOtherAudioPlaying = false if audio.supportsSessionProperty == true then if not(audio.getSessionProperty(audio.OtherAudioIsPlaying) == 0) then isOtherAudioPlaying = true player.music = false end end
Thanks,
-Corey Simpson
Ah, thanks, Corey. I didn’t look at Andrew’s linked post. It sounds like I need to update my code.
Cheers,
Naomi
Hey, Andrew @aukStudios, about your post in the other thread (http://forums.coronalabs.com/topic/8742-new-audiosession-properties/page-3#entry144436), I have a question regarding the on suspend and resume events.
I assume I need the runtime system event listener in main.lua, and on “applicationSuspend”, I’d call audio.stop, and on “applicationResume”, I’d check the flag for other audio, and if other audio is playing, I won’t play the music. Does this cover the basic flow? (My theme music is loaded globally and I don’t intend to dispose and re-load upon suspend/resume events.)
Naomi
Hey Naomi,
Yep, that’s the right flow.
My recollection is that, on suspend events, it was also necessary to actually dispose of the audio via audio.dispose() after stopping it via audio.stop(). Merely calling audio.stop() wasn’t sufficient to ensure that, when the application resumed, audio.OtherAudioIsPlaying would give the right result (i.e., correctly indicate whether iTunes is playing any music). But I haven’t tested it for a long time, so it’s possible that audio.dispose() may not have been necessary after all. I’d be curious to know if that’s what you find.
- Andrew
Thank you, Andrew! I’m wondering if you dispose all sound effects when you dispose music.
The thing is, if you don’t dispose the sound effects, then I imagine it’s okay to keep the music loaded in memory too… The way I do it, I have all required sound effects and music loaded in memory and play them when required. So, if it’s okay to keep the sound effects in memory for the flag (OtherAudioIsPlaying) to work correctly, then I’ll keep the music loaded too. However, if you dispose of all loaded sound effects and music, and then load them again upon applicationResume, then I suppose that’s how it needs to be done…
I do not wish to dispose all sound files and then re-load them upon applicationResume – so if it needs to be so, I may do away with runtime event listener bit. My settings menu allows user to have music & sound effects or sound effects only or none. So if a user wants to play their own music, they could just turn off the music from my game while keeping the sound effects after all…
Naomi
Hi Naomi,
No, I don’t dispose of all my sound effects when the app suspends. I only dispose the music. And even that may not actually be necessary (I thought it was at the time, but I haven’t tested it since then).
- Andrew