Exception at AVAudioEngine startup

Dear all,


I've been working, for quite a while now, on the migration of all my iOS musical apps from AUGraph/AudioToolbox to AVAudioEngine/AVFoundation.


We released an app recently with this update and discovered a new source of exceptions intoduced by AVAudioEngine (logs with Fabric's Crashlytics).


The app is basically an audio player (with lots of fancy features requiring signal processing code embedded in a custom audio unit) and provides background audio. The AVAudioSession is in Payback category with no option, as it's not supposed to mix with other sources. Everything is embedded in a single custom AudioUnit directly connected to the OutputNode inside an AVAudioEngine.


I have two new weird exceptions I don't know how to interpret, especially as they are not reproductible at all, and concerns mostly old devices.


1. At the end of an interruption

When I receive the InterruptionBegan notification, I stop the AVAudioEngine.

When I receive the InterruptionEnded notification, I start back the AVAudioEngine.

This last action causes, sometimes, an exception with code 560557684 (AVAudioSessionErrorCodeCannotInterruptOthers). This happens mostly on iPhone 5. But as I said, it is not reproductible and works like a charm most of the times.

I was able to reproduce it once on an iPhone 5 by starting the app, starting a song, putting the app on background, and then calling the phone through FaceTime. I got the error during the InterruptionEnded notification, even though no other app was running at that time.


2. At startup

This one is the weirdest for me. Sometimes, starting the AVAudioEngine during the app startup (inside didFinishLaunchingWithOptions) causes the same error (560557684: AVAudioSessionErrorCodeCannotInterruptOthers). This happen mostly on iPhone 4s and iPhone 5, and I never managed to reproduce it.


The only page I found about a similar problem is the following:

https://stackoverflow.com/questions/29036294/avaudiorecorder-not-recording-in-background-after-audio-session-interruption-end

But the solutions here are not very satisfying as :

- I don't want my app to mix with others, and once again, it all works most of the time.

- My app already uses remote control events so this doesn't solve anything.


My questions :

- Did anybody encounter a similar problem?

- I don't event know how to start to deal with that, any advice?


Thank you 🙂


Tom

Replies

I encounter the 560557684 (AVAudioSessionErrorCodeCannotInterruptOthers) error too after i stop audio in the other app and send that other app to the background.
When you receive AVAudioSessionInterruptionTypeEnded notification, could you check the userInfo dictionary for AVAudioSessionInterruptionOptionKey, and resume audio playback only if the option value is AVAudioSessionInterruptionOptionShouldResume.