Hi Rob,
Yes, this is a tricky problem. Here’s what has worked for me.
First, at or very near the top of your main.lua file, put something like the following. I’ve found it’s necessary to run this before you make any audio API calls (even loading sounds or streams), or it won’t work.
[blockcode]
– Set the audio mix mode to allow sounds from the app to mix with other sounds from the device
if audio.supportsSessionProperty == true then
audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)
end
– Store whether other audio is playing. It’s important to do this once and store the result now, as referring to 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
end
end
[/blockcode]
As a result of the mix mode, if the app is launched while the device is playing music, the music will continue to play. Moreover, when you play audio in your own app, it will mix with the audio that the device is already playing, which is typically what you would want to happen for sound effects. If your app has background music, you should play it only if isOtherAudioPlaying is false (otherwise your music will play at the same time as the music already playing on the device).
But that’s not all. To get this to work on suspend and resume events, you need to do a bit more.
In your appSuspend event, call audio.stop on your music channel and audio.dispose on your music handle. This ensures that when the app is resumed, audio.OtherAudioIsPlaying does not give the wrong result.
Then, in your appResume event, use the same exact code as above, and reload your music file (since it was disposed of when the app was suspended).
Hope this helps.
- Andrew [import]uid: 109711 topic_id: 10882 reply_id: 128035[/import]