From an app that reads audio from the built-in microphone, I'm receiving many crash logs where the AVAudioEngine fails to start again after the app was suspended.
Basically, I'm calling these two methods in the app delegate's
applicationDidBecomeActive
and
applicationDidEnterBackground
methods respectively:
let audioSession = AVAudioSession.sharedInstance()
func startAudio() throws {
self.audioEngine = AVAudioEngine()
try self.audioSession.setCategory(.record, mode: .measurement)}
try audioSession.setActive(true)
self.audioEngine!.inputNode.installTap(onBus: 0, bufferSize: 4096, format: nil, block: { ... })
self.audioEngine!.prepare()
try self.audioEngine!.start()
}
func stopAudio() throws {
self.audioEngine?.stop()
self.audioEngine?.inputNode.removeTap(onBus: 0)
self.audioEngine = nil
try self.audioSession.setActive(false, options: [.notifyOthersOnDeactivation])
}
In the crash logs (iOS 16.6) I'm seeing that this works fine several times as the app is opened and closed, but suddenly the audioEngine.start()
call fails with the error
Error Domain=com.apple.coreaudio.avfaudio Code=-10851 "(null)" UserInfo={failed call=err = AUGraphParser::InitializeActiveNodesInInputChain(ThisGraph, *GetInputNode())}
and the audioEngine!.inputNode.outputFormat(forBus: 0)
is something like
<AVAudioFormat 0x282301c70: 2 ch, 0 Hz, Float32, deinterleaved>
. Also, right before installing the tap, audioSession.availableInputs
contains an entry of type MicrophoneBuiltIn
but audioSession.currentRoute
lists no inputs at all.
I was not able to reproduce this situation on my own devices yet.
Does anyone have an idea why this is happening?