ApplicationMusicPlayer Audio Session Issue When Switching to AVAudioEngine in Background

Hi! I'm developing a music player app that interchanges between ApplicationMusicPlayer and AVAudioEngine. I'm facing an issue when switching from playback via ApplicationMusicPlayer to AVAudioEngine while the app is in background. Based on testing, it seems like the issue has to do with being unable to set audio focus in background, causing error AVAudioSessionErrorCodeCannotInterruptOthers.

I would like to check if ApplicationMusicPlayer has its own audio focus separated from the app's own audio focus. If it is, is there anything that I can do to ensure that ApplicationMusicPlayer returns focus to the app?

(I notice that the issue does not occur if we are moving playback from AVAudioEngine to ApplicationMusicPlayer. Not sure why the opposite does not work)

The documentation for AVAudioSessionErrorCodeCannotInterruptOthers mentions that this error

[...] indictates an attempt to make a nonmixable audio session active while the app was in the background.

So likely the error is due to your app's audio session not being mixable, that is, not including the AVAudioSessionCategoryOptionMixWithOthers option, for example.

ApplicationMusicPlayer Audio Session Issue When Switching to AVAudioEngine in Background
 
 
Q