Background audio, multiple AirPlayer and AirPlay 2

I have a working application that uses multiple AVPlayer to play audio, each player holds a song and I play() another AVPlayer to play the next song (this is to allow realtime mixing between the 2 songs).

The application works perfectly in foreground and background, unless the audio output is an AirPlay 2 speaker (I'm using a _new_ appleTV).

If the output is AirPlay 2, as soon the app goes in background (or the song changes, changing AVPlayer), audio stops and the app is apparently stopped (It do not listen anymore to notification observations and the such). As soon as I move the app in foreground the pending notifications and observation starts. The stop happens ca. 80% of the times, it's not every time, but frequently enough.

I tried anything I could imagine: Audio Session is now set to: [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongForm options:0 error:&error] (in case the route sharing policy interferes with everything) and set active. I also implemented AVRoutePickerView instead ov MPVolumeView as audio picker, just in case... and I also tried AVAudioSessionModeMoviePlayback mode (this is a sign I'm going into the magic realm).

This is something related to AirPlay 2, because the application works in any other audio output context.

Any hint/help will be appreciated, this is really frustrating.

Replies

Do you use the mixwithother flag on the audiosession?

Did you ever figure this out? I'm having this exact same issue and it's maddening.

Not at all. I just gave up, the server I'm using was changing API so I was in for a serious rewrite, I just used AVQueuePlayer that do not seem to have the problem.

Sorry to bring bad news.

No, it's an audio player. It just grab the audio seession and play.