AUGraphInitialize() fails when app is backgrounded

I'm using AUGraph to set up my playback engine. I know AUGraph is marked with API_TO_BE_DEPRECATED in favor of AVAudioEngine

but it works perfect for my needs and has for many years.


My player (as most players) may play audio in the background and a new song will be played when the previous ends.

This works fine if both songs are using kAudioUnitSubType_RemoteIO for playback (setup using an AUGraph)

meaning that tearing down and setting up a new audo output works also when the app is backgrounded.


However AUGraphInitialize() fails when the app is backgrounded if the previous song was played using MPMusicPlayerController.

So the switch from MPMusicPlayerController to kAudioUnitSubType_RemoteIO makes it fail.


Fails with '!int' error: AUGraphInitialize() 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>)


This problem only occur if the app is backgrounded, else it works fine.


According to the thread below

https://forums.developer.apple.com/thread/38917

it's not always possible to initialize an kAudioUnitSubType_RemoteIO when the app is backgrounded.

One suggestion is to use AVAudioSessionCategoryOptionMixWithOthers which makes it work also when backgrounded but it's not any good solution.


It would be nice if any of the Apple audio engineers could explain if there is a way to make this work?

Would a change AVAudioEngine make it work (I guess not since it's probably still using kAudioUnitSubType_RemoteIO under the hood).

I need to use MPMusicPlayerController since some files are DRM protected and for other files I need to use kAudioUnitSubType_RemoteIO

to be able to process the audio samples.


Not having this working when the app is backgrounded makes for a very bad user experience.