MPMediaItem and AVPlayerItem playback sequence in background

I'm having an issue with playing sequences of different kinds of items in a background.

In an app I'm working on we've introduced playlists which contain both content provided by the app and Apple Music content.

For that use AVPlayer and MPMusicPlayerController respectively. We observe one player or the other (depending what content is now playing) and if the other kind of content comes next, we release the old player (if we can - MPMusicPlayerController is a singleton, so best we can do is stop it) and load item to another player.
The problem starts when the app leaves foreground. Once MPMusicPlayerController takes over it doesn't want to give up control, so if any AVPlayer content comes after MPMusicPlayerController content, the music stops.


One workaround that I've tried is playing with `.mixWithOthers` options when I set the category on AVAudioSession, however this creates new category of problems - I'm loosing lockscreen controls, therefore I'm also loosing airplay. One dirty trick that I've tried was setting `.mixWithOthers` 3 seconds before MPMediaItem ends, and then disabling it back once AVPlayer starts. Beside the fact that there're probably many different things that can go wrong here, MPMediaPlayerController still doesn't want to give me back the control over lockscreen controls.


Is there any way this could ever work on iOS 13?

Did you manage to find a solution to this? Or any workarounds?

I see the same problem but in my case I'm trying to create a Core Audio class using kAudioUnitSubType_RemoteIO for playback,

when backgrounded it fails like this:

Fails with '!int' error: AudioUnitInitialize() failed. ('!int' == 560557684) [aurioc] AURemoteIO.cpp:1086:Initialize: failed: 560557684

(enable 2, outf< 2 ch, 44100 Hz, Int16, inter> inf< 2 ch, 0 Hz, Float32, non-inter>)


In foreground it works OK.

I've filled a bug report: FB7622685

It's important that more people report this problem, eventually it might get fixed (hopefully).


I'm sure it's the same error regardless if using AVPlayer or kAudioUnitSubType_RemoteIO. Any high level audio class most probably uses kAudioUnitSubType_RemoteIO under the hood.


It would be nice to get a comment from some Apple engineer

No. To this day nothing better than what I've described above.

3 years later, this is still a show-stopping issue for many different use cases. Did Apple ever respond to your ticket?

Was there ever a mechanism found to get around this like forcing control away from the MPMusicPlayerController? I've tried tearing down my instance, ending event listeners, brute forcing the audio session to be mixed with others...nothing.

Still the same problem. Works perfectly in the foreground and not at all in the background.

MPMediaItem and AVPlayerItem playback sequence in background
 
 
Q