watchOS: Resume recording from AudioInterruption in background mode

Hi,

I have a watchOS app that records audio for an extended period of time and because the mic is active, continues to record in background mode when the watch face is off. However, when a call comes in or Siri is activated, recording stops because of an audio interruption. Here is my code for setting up the session:

private func setupAudioSession() {
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(.playAndRecord, mode: .default, options: [.overrideMutedMicrophoneInterruption])
            try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
        } catch {
            print("Audio Session error: \(error)")
        }
    }

Before this I register an interruption handler that holds a reference to my AudioEngine (which I start and stop each time recording is activated by the user):

_audioInterruptionHandler = AudioInterruptionHandler(audioEngine: _audioEngine)

And here is how this class implements recovery:

fileprivate class AudioInterruptionHandler {
    private let _audioEngine: AVAudioEngine

    public init(audioEngine: AVAudioEngine) {
        _audioEngine = audioEngine

        // Listen to interrupt notifications
        NotificationCenter.default.addObserver(self, selector: #selector(handleAudioInterruption(notification:)), name: AVAudioSession.interruptionNotification, object: nil)
    }

    @objc private func handleAudioInterruption(notification: Notification) {
        guard let userInfo = notification.userInfo,
              let interruptionTypeRawValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
              let interruptionType = AVAudioSession.InterruptionType(rawValue: interruptionTypeRawValue) else {
            return
        }

        switch interruptionType {
        case .began:
            print("[AudioInterruptionHandler] Interruption began")
        case .ended:
            print("[AudioInterruptionHandler] Interruption ended")
            print("Interruption ended")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
            } catch {
                print("[AudioInterruptionHandler] Error resuming audio session: \(error.localizedDescription)")
            }
        default:
            print("[AudioInterruptionHandler] Unknown interruption: \(interruptionType.rawValue)")
        }
    }
}

Unfortunately, it fails with:

Error resuming audio session: Session activation failed

Is this even possible to do on watchOS? This code worked for me on iOS.

Thank you,

-- B.

Replies

Recording cannot be resumed when the app is in the background on watchOS. It must be a user-initiated event while the app is in the foreground. (Recording can then continue once the app moves to the background.)

Thank you. One follow-up question in light of this:

Is user-initiated long-term recording a supported use case permissible in the app store? I ask because the documentation for .playAndRecord and audio background mode tends to focus almost exclusively on audio playback, with the recording capability seemingly implied to be to support communication.