I've also just run into this same problem. I have two AVPlayer objects, one which plays music and the other plays a narration.
The two AVPlayers stream just fine with Bluetooth and AirPods. But on AirPlay 2 devices like a Sonos speaker, only one or the other will stream, which was an unexpected and not very nice surprise.
The only thing I found to work is to configure your AVAudioSession to not support AirPlay 2 (i.e., don't use the .longFormAudio policy), and make sure to use the .duckOthers option, as follows:
Code Block try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.duckOthers]) |
This will stream both AVPlayers' content to an AirPlay 2 device like Sonos. But, the downside is you lose the lock screen / Control Center playback controls, due to the .duckOthers option. Ultimately I guess this is better than the alternative of my app being non-compatible with Sonos/HomePod. But, losing the lock screen controls is a major bummer.
Curious if anyone else has any other options, short of the suggestions above which involve a major and costly rewrite using AVSampleBufferAudioRenderer.
I would think this multiple AVPlayer pattern is a common pattern in meditation apps, as you want the music to continue looping after the narration ends, and you might want to allow the user to change the relative volume between the music AVPlayer and the narration AVPlayer. So, I'm surprised this doesn't work with AirPlay 2.
I looked at the Headspace app and they also don't support AirPlay with simultaneous narration+music, so I'm guessing they couldn't figure out a solution either?