Anton
Anton

Reputation: 3

Diagnosing Swift AVAudioRecorder domain error

I'm trying to create an audio recording class. When I instantiate AVAudioRecorder I am getting the error

Error Domain=NSOSStatusErrorDomain Code=1718449215 "(null)"

which is not very helpful.

I've attached my code below. I'm trying to do something similar to the top answer in this question.
Also I'm keen to learn, so if you feel like critiquing other aspects of my code I would also appreciate that.

class Recorder : NSObject, AVAudioRecorderDelegate {
    let recordingName: String
    var audioRecorder: AVAudioRecorder?
    var isRecording = false
    
    init(recordingName: String) {
        self.recordingName = recordingName
        super.init()
        self.audioRecorder = setUpRecorder()
        requestRecordPermission()
    }
    
    func filePath() -> URL {
        let fileName = self.recordingName + ".mp3"
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory.appendingPathComponent(fileName)
    }
    
    func requestRecordPermission() {
        AVAudioApplication.requestRecordPermission(completionHandler: {permission in
            if !permission {
                print("This application doesn't have permission to record audio")
            } else {
                print("Application has permission to record audio")
            }
        })
    }
    
    func recordPermission() -> Bool {
        let permissionStatus = AVAudioApplication.shared.recordPermission
        switch permissionStatus {
        case .granted:
            return true
        case .denied:
            return false
        case .undetermined:
            print("audio permission is undetermined")
            return false
        @unknown default:
            print("record permission function needs updating")
            return false
        }
    }
    
    func setUpRecorder() -> AVAudioRecorder? {
        if recordPermission() {
            let session = AVAudioSession.sharedInstance()
            do {
                try session.setCategory(.record)
                try session.setActive(true)
                let settings = [
                    AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                    AVSampleRateKey: 44100,
                    AVNumberOfChannelsKey: 2,
                    AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
                ]
                let audioRecorder = try AVAudioRecorder(url:filePath(), settings: settings)
                audioRecorder.delegate = self
                audioRecorder.isMeteringEnabled = true
                audioRecorder.prepareToRecord()
                return audioRecorder
            } catch let error {
                print("Recorder set up failed: \(error)")
            }
        }
        return nil
    }
    
    func toggleRecording() {
        if let recorder = self.audioRecorder {
            if (isRecording) {
                recorder.stop()
                self.isRecording = false
            } else {
                if recorder.record() {
                    self.isRecording = true
                }
            }
        }
    }
}

Tried: I couldn't find too much online about this specific error code. GPT says it means no active audio session. I haven't managed to get much further than that.

Expect: setUpRecorder() returns a new AVAudioRecorder that I can use in my toggle method.

Upvotes: 0

Views: 84

Answers (0)

Related Questions