avaudiosessionavaudiorecorderavaudioengine

AVAudioSession_iOS.mm:2382 Failed to set category, error: -50 when category set to .record


do {
        // Activate the session.
        audioSession = AVAudioSession.sharedInstance()
        guard let audioSession = audioSession else { return }
        try audioSession.setActive(false)
        try audioSession.setCategory(.playAndRecord, mode: .spokenAudio)    //forced
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

        // Start the processing pipeline.
        try audioEngine.start()
        Task { @MainActor in
            isRecording.wrappedValue = true
        }
    } catch {
        handleError(withMessage: error.localizedDescription)
    }

For some reason audioSession is only allowed for .playAndRecord. Instead if I set it to .record I will get this error

AVAudioSession_iOS.mm:2382 Failed to set category, error: -50

I'm only recording, my app doesn't play audio. It's kinda a workaround for now. But is that normal?


Solution

  • .spokenAudio mode implies that you're going to produce audio, so AVAudioSession.setCategory returning -50 = AVAudioSessionErrorCodeBadParam is not unreasonable.

    Set the mode to .default instead.