Implementing interface sounds on iOS and Mac Catalyst

30 Views Asked by At

I am using AVAudioSession and AVAudioPlayer to play interface sounds in our app:

try self.audioSession.setCategory(.ambient, options: [.mixWithOthers])
try self.audioSession.setActive(true)

If I use .mixWithOthers then the sounds play nicely along with a music playing on the same device. But if I have music playing on my phone via AirPods and running the Mac Catalyst version of the app, Mac will switch audio source to the computer and stop my music.

For this I've found that .defaultToSpeaker fixes this issue and prevents AirPods to connect to the device which just started playing it.

The problem is that default to speaker is not a valid option along with mix with others, if I want to use them both, I get a warning and only one of them will be active, meaning my interface sounds will stop music playing on the same device.

I've also checked out AudioServicesPlaySystemSoundWithCompletion, but documentation says that it lacks playing multiple sounds at the same time, which is not good for my use case, since I have samples 1-2 seconds long, which can definitely overlap:

The interface does not provide level, positioning, looping, or timing control, and does not support simultaneous playback: You can play only one sound at a time

What do you recommend? How can one solve this?

0

There are 0 best solutions below