Resuming audio after an interruption when the app is in background

This is what I did:
  • Set AVAudioSession to active and its category to .playback on app launch

  • Registered to observe notifications of type interruptionNotification

  • Added Background Modes in the project settings under Signing & Capabilities

When I get an interruption I check under case .ended: if the music was playing before the interruption. If so, I resume the playback, something like this (correct me if I'm wrong):
Code Block
if options.contains(.shouldResume) {
if musicWasPlaying {
audioPlayer.play()
}
}

This seems to be working fine when the app is in foreground. But, when it's in background and I get an interruption, the audio doesn't resume and I get this error:
Code Block
[aurioc] 1540: AUIOClient_StartIO failed (560557684)
error 2 Error Domain=NSOSStatusErrorDomain Code=560557684 "(null)"

Is there a way to resume the audio in this specific state?
Have you found a way to make this work ?

I have a very similar problem. When I get an audio interruption while in background, calling AudioOutputUnitStart fails and I get this error: [aurioc] AURemoteIO.cpp:1590:Start: AUIOClient_StartIO failed (560557684)

Like you, I do have the proper background mode enabled.


The error indicates that another app (presumably the one in the foreground) has an active audio session, and your non-mixable app isn't allowed to interrupt it by starting playback. If you want to be able to start playing from the background, you should use a category option that makes your app mixable.

If your app shouldn't be mixable, then you should deactivate your audio session when moving into the background, and reactivate it when moving into the foreground.

If you want some kind of "hybrid" behavior, where you app is non-mixable but you want playback to continue when it's sent to the background, at least until something interrupts it, then I guess the correct behavior is *not* to resume playing while you're still in the background. Instead, remember that you want to resume playing, and do it when you move into the foreground.

This topic is discussed here: https://developer.apple.com/documentation/avfoundation/avaudiosession/1616596-interruptionnotification
Resuming audio after an interruption when the app is in background
 
 
Q