停止并开始录制声音时崩溃

时间:2017-04-20 09:09:19

标签: ios swift

我正在使用以下代码录制语音。

open func startRecording() throws {
    if let recognitionTask = recognitionTask {
        recognitionTask.cancel()
        self.audioEngine.stop()
        self.recognitionTask = nil
        self.recognitionRequest = nil
        self.recognitionTask = nil
    }

    let audioSession = AVAudioSession.sharedInstance()
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with:.defaultToSpeaker)
    try audioSession.setMode(AVAudioSessionModeMeasurement)
    try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
    print("Available Inputs ---> %@" , audioSession.availableInputs!)

    self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

    guard let inputNode = self.audioEngine.inputNode else { fatalError("Audio engine has no input node") }
    guard let recognitionRequest = self.recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }

    // Configure request so that results are returned before audio recording is finished
    recognitionRequest.shouldReportPartialResults = true
    recognitionRequest.contextualStrings = ["Zeva","Zeva"];
    recognitionRequest.taskHint = .confirmation
    // A recognition task represents a speech recognition session.
    // We keep a reference to the task so that it can be cancelled.
    recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
        var isFinal = false
        if let result = result {
            isFinal = result.isFinal
            self.delegate?.speechRecognitionPartialResult(result.bestTranscription.formattedString)
        }

        if error != nil || isFinal {
            self.audioEngine.stop()
            inputNode.removeTap(onBus: 0)

            self.recognitionRequest = nil
            self.recognitionTask = nil
        }

        if isFinal {
            let speechResult = result!.bestTranscription.formattedString
            let filteredResult =  self.filterUnidentifiedWords(text: speechResult)
            self.delegate?.speechRecognitionFinished(filteredResult)
            self.stopRecording()
        }
        else {
            if error == nil {
                self.restartSpeechTimeout()
            }
            else {
                print("(ST)",error?.localizedDescription ?? "Some Error")
                CommonUtility.showToast("Something went wrong. Please try again. (ST)")
            }
        }
    }

    let recordingFormat = inputNode.outputFormat(forBus: 0)
    inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
        self.recognitionRequest?.append(buffer)
    }
    print(recordingFormat,recognitionRequest)
    self.audioEngine.prepare()

    try self.audioEngine.start()
}

但是当我停止并开始录制时,它会在上面的代码中的以下位置self.audioEngine.prepare()崩溃,并显示以下消息。 如何处理崩溃?

  

  致命错误:在展开Optional值时意外发现nil   2017-04-20 14:24:23.170698 ZevaApp [408:51773]致命错误:在展开可选值时意外发现nil

0 个答案:

没有答案