Issue with AVAudioEngine and AVAudioSession after Interruption and Background Transition - 561145187 error code

Description:

I am developing a recording-only application that supports background recording using AVAudioEngine. The app segments the recording into 60-second files for further processing. For example, a 10-minute recording results in ten 60-second files.

Problem:

The application functions as expected in the background. However, after the app receives an interruption (such as a phone call) and the interruption ends, I can successfully restart the recording. The problem arises when the app then transitions to the background; it fails to restart the recording. Specifically, after ending the call and transitioning the app to the background, the app encounters an error and is unable to restart AVAudioSession and AVAudioEngine. The only resolution is to close and restart the app, which is not ideal for user experience.

Steps to Reproduce:

1.	Start recording using AVAudioEngine.
2.	The app records and saves 60-second segments.
3.	Receive an interruption (e.g., an incoming phone call).
4.	End the call.
5.	Transition the app to the background.
6.	Transition the app to the foreground and the session will be activated again.
7.	Attempt to restart the recording.

Expected Behavior:

The app should resume recording seamlessly after the interruption and background transition.

Actual Behavior:

The app fails to restart AVAudioSession and AVAudioEngine, resulting in a continuous error. The recording cannot be resumed without closing and reopening the app.

How I’m Starting the Recording:

Configuration:

internal func setAudioSessionCategory() {
    do {
      try audioSession.setCategory(
        .playAndRecord,
        mode: .default,
        options: [.defaultToSpeaker, .mixWithOthers, .allowBluetooth]
      )
    } catch {
      debugPrint(error)
    }
  }

  internal func setAudioSessionActivation() {
    if UIApplication.shared.applicationState == .active {
      do {
        try audioSession.setPrefersNoInterruptionsFromSystemAlerts(true)
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

        if audioSession.isInputGainSettable {
          try audioSession.setInputGain(1.0)
        }
        try audioSession.setPreferredIOBufferDuration(0.01)
        try setBuiltInPreferredInput()
      } catch {
        debugPrint(error)
      }
    }
  }

Starting AVAudioEngine:

internal func setupEngine() {
    if callObserver.onCall() { return }

    inputNode = audioEngine.inputNode
    audioEngine.attach(audioMixer)
    audioEngine.connect(inputNode, to: audioMixer, format: AVAudioFormat.validInputAudioFormat(inputNode))
  }



internal func beginRecordingEngine() {
    audioMixer.removeTap(onBus: 0)
    audioMixer.installTap(onBus: 0, bufferSize: 1024, format: AVAudioFormat.validInputAudioFormat(inputNode)) { [weak self] buffer, _ in
      guard let self = self, let file = self.audioFile else { return }
      write(file, buffer: buffer)
    }
    audioEngine.prepare()

    do {
      try audioEngine.start()

      recordingTimer = Timer.scheduledTimer(withTimeInterval: recordingInterval, repeats: true) { [weak self] _ in
        self?.handleRecordingInterval()
      }
    } catch {
      debugPrint(error)
    }
  }

On the try audioEngine.start() call, I receive error code 561145187 in the catch block.

Logs/Error Messages:

•	Error code: 561145187

Request:

I would appreciate any guidance or solutions to ensure the app can resume recording after interruptions and background transitions without requiring a restart.

Thank you for your assistance.

Answered by Engineer in 800001022

Error code 561145187 (!rec) corresponds to AVAudioSession.ErrorCode.cannotStartRecording. It usually happens when trying to start a recording while the app is in the background. If the error is being produced while the app is in the foreground, there could be something else going on.

If your app received a mediaServicesWereResetNotification, for example, you need to:

Respond to these events by reinitializing your app’s audio objects (such as players, recorders, converters, or audio queues) and resetting your audio session’s category, options, and mode configuration.

It is also important to register for the AVAudioEngineConfigurationChange notification. Although your app might not need to reallocate its audio engine, it will likely need to reestablish any node connections if the formats have changed.

I have the same issue. I'm recording using AVAudioEngine, I listen for interruptions. If a call come in and its answered, then hangs up, I can't restart the recording. I think the audio input changes to the phone app and I can't seem to get it back when returning to my app. I would rather not have to recreate the whole engine if possible.

Accepted Answer

Error code 561145187 (!rec) corresponds to AVAudioSession.ErrorCode.cannotStartRecording. It usually happens when trying to start a recording while the app is in the background. If the error is being produced while the app is in the foreground, there could be something else going on.

If your app received a mediaServicesWereResetNotification, for example, you need to:

Respond to these events by reinitializing your app’s audio objects (such as players, recorders, converters, or audio queues) and resetting your audio session’s category, options, and mode configuration.

It is also important to register for the AVAudioEngineConfigurationChange notification. Although your app might not need to reallocate its audio engine, it will likely need to reestablish any node connections if the formats have changed.

Issue with AVAudioEngine and AVAudioSession after Interruption and Background Transition - 561145187 error code
 
 
Q