Stopping external music

Hello,

I have a game with music in it and I am wondering whether or not there is a way to have the game music not end audio from other apps (iTunes, podcasts, etc…). Currently, even when I set the audio volume to zero, the app still stops any music from other apps that are playing. This is a big turn off for me with other apps so I want to fix it. In other words, what part of the audio library turns off external music? Is it the require(“audio”) call, the audio.loadstream() function, the audio.play() function, or something else?

Thank you in advance for any help you can give.

Is the background music of your game in MP3 format? If so, read on –

I think iOS devices only allow a single MP3 (encoded) file to be played at any given instant because of a hardware decoding limitation. So if there is a song playing in your native music app, and then your game is fired up by the user, the background music of your game (in MP3 format) would take over, thus muting out the already playing song.

Try converting your background music to an uncompressed format like WAV or CAF and give it a spin.

I’m just spitballing here, I could be wrong. So you’ll have to test this.

If I understand you correctly you want the external music to keep playing, right?

In that case you can add this to your main.lua

local otherAudioIsPlaying = false       -- store whether other audio is playing when app is started if (audio.supportsSessionProperty) then -- set the audio mix mode to allow sounds from the app to mix with other sounds from the device audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode) if (audio.getSessionProperty(audio.OtherAudioIsPlaying) ~= 0) then otherAudioIsPlaying = true end end

You can then use otherAudioIsPlaying to determine if the user is listening to something else and tell your game *not* to start playing the game music so it won’t interfere with music the user is listening to.

This works for iOS and Android.

I am receiving false from audio.supportsSessionProperty on Android platforms.

@ingemar would there be additional build.settings configurations we need to enable such detection or Android simply just don’t not qualify for that support?

I have to admit that Android is a secondary platform for me as iOS is what I mainly focus on.
Having said that I do recall this feature working on Android as well. It may be that Android supports this “out-of-the-box” without any special handling in Corona. The only thing that won’t work in that case is determining if audio is playing when the app is started.

I did both of these and it works perfectly. Thank you!

@ian

Sounds great! 

The reason for the code above is that Corona needs to be told not to stop any external audio, and that what my snippet does.

Not using MP3 is a good idea anyway to get rid of any potential licensing issues down the road. For music I use OGG for Android and MPEG-4 (m4a) for iOS. For short sounds I use WAV on both platforms.

OK I see @ingemar , no worries and thanks for the tips ! 

Is the background music of your game in MP3 format? If so, read on –

I think iOS devices only allow a single MP3 (encoded) file to be played at any given instant because of a hardware decoding limitation. So if there is a song playing in your native music app, and then your game is fired up by the user, the background music of your game (in MP3 format) would take over, thus muting out the already playing song.

Try converting your background music to an uncompressed format like WAV or CAF and give it a spin.

I’m just spitballing here, I could be wrong. So you’ll have to test this.

If I understand you correctly you want the external music to keep playing, right?

In that case you can add this to your main.lua

local otherAudioIsPlaying = false       -- store whether other audio is playing when app is started if (audio.supportsSessionProperty) then -- set the audio mix mode to allow sounds from the app to mix with other sounds from the device audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode) if (audio.getSessionProperty(audio.OtherAudioIsPlaying) ~= 0) then otherAudioIsPlaying = true end end

You can then use otherAudioIsPlaying to determine if the user is listening to something else and tell your game *not* to start playing the game music so it won’t interfere with music the user is listening to.

This works for iOS and Android.

I am receiving false from audio.supportsSessionProperty on Android platforms.

@ingemar would there be additional build.settings configurations we need to enable such detection or Android simply just don’t not qualify for that support?

I have to admit that Android is a secondary platform for me as iOS is what I mainly focus on.
Having said that I do recall this feature working on Android as well. It may be that Android supports this “out-of-the-box” without any special handling in Corona. The only thing that won’t work in that case is determining if audio is playing when the app is started.

I did both of these and it works perfectly. Thank you!

@ian

Sounds great! 

The reason for the code above is that Corona needs to be told not to stop any external audio, and that what my snippet does.

Not using MP3 is a good idea anyway to get rid of any potential licensing issues down the road. For music I use OGG for Android and MPEG-4 (m4a) for iOS. For short sounds I use WAV on both platforms.

OK I see @ingemar , no worries and thanks for the tips ! 

Hi guys,

I tried your solution to have the background music keep playing mixed with my app sounds. I put this in main:

local otherAudioIsPlaying = false       -- store whether other audio is playing when app is started if (audio.supportsSessionProperty) then -- set the audio mix mode to allow sounds from the app to mix with other sounds from the device audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode) if (audio.getSessionProperty(audio.OtherAudioIsPlaying) ~= 0) then otherAudioIsPlaying = true end end

and had my audio files converted to Wav format. Audio gets mixed properly BUT at the first app start the external music gets quit. If I bring up the control center it immediately gets restarted without even need to press play, but I’d like to have it playing without interruptions even at first app opening. Any idea on why this is happening and where to modify this snippet in order to prevent such behavior??

Thank you!!!

 

I have this top of main.lua and it works:

[lua]require(‘audio’)  

if audio.supportsSessionProperty then  

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

end  [/lua]

I’m not sure if the “require” is required or not. That seems to be the only difference between this and yours though. Give it a go.

Hi @jonjonsson,

thank you for the sinppet. I tried but the same problem problem shows up: external audio quits on first app launch.

Is there something I might need to add, other than the snippet to main.lua, in build or config to make it work?

Thank you!!!

I just verified that it’s working for me. I don’t have anything in build settings pertaining to audio.

I guess I would try the following:

Remove if (audio.getSessionProperty(audio.OtherAudioIsPlaying) 

perhaps it has a bug that stops audio? 

If all else fails, make a blank project with only the above code and see if it works as expected or if you still have the problem.

If it still doesn’t work it may be a difference in what version of Corona you are using.

thank you @jonjonsson,

sorry could you post the snippet without the supposed to be bugged line?

I don’t have any reference to audio in settings either…