SFSpeechRecognizer kAFAssistantErrorDomain代码= 203“重试”

时间:2018-01-08 03:35:42

标签: ios swift sfspeechrecognizer

我使用SFSpeechRecognizer,基本上可以工作。

1.但有时会发生以下错误。   而且大部分时间我没有执行avStop()

  

[Utility] + [AFAggregator logDictationFailedWithError:]错误   Domain = kAFAssistantErrorDomain Code = 203“Retry”   UserInfo = {NSLocalizedDescription = Retry,NSUnderlyingError = 0x1c464b880   {Error Domain = SiriSpeechErrorDomain Code = 1“(null)”}}

2.完全无法在后台工作,会产生以下错误。

  

[Utility] + [AFAggregator logDictationFailedWithError:] Error Domain = kAFAssistantErrorDomain Code = 1700“(null)”

class MySpeech:NSObject{
 private var iosRecognizer: SFSpeechRecognizer?
 private var iosRequest: SFSpeechAudioBufferRecognitionRequest?
 private var iosTask: SFSpeechRecognitionTask?
 private let iosAVE = AVAudioEngine()
 private let avSession = AVAudioSession.sharedInstance()

 func avINIT(){
    try? avSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth])
    try? avSession.setMode(AVAudioSessionModeMeasurement)
    try? avSession.setActive(true, with: .notifyOthersOnDeactivation)
 }
 func switchHFP(){
    do{
        //try avSession.setActive(false)
        try avSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth])
        try avSession.setActive(true, with: .notifyOthersOnDeactivation)
    } catch {
        debugPrint("HFP error: \(error.localizedDescription)")
    }
}
 func avStart(_ sLNG:NSString){
        if let iosTask = iosTask {
            iosTask.cancel()
            self.iosTask = nil
        }
        iosRecognizer=SFSpeechRecognizer(locale: Locale(identifier:sLNG as String))!
        iosRequest = SFSpeechAudioBufferRecognitionRequest()

        guard let inputNode = iosAVE.inputNode else { fatalError("Audio engine has no input node") }

        guard let recognitionRequest = iosRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }

        recognitionRequest.shouldReportPartialResults = false

        iosTask = iosRecognizer?.recognitionTask(with: recognitionRequest) { result, error in
            if let result = result {
                if result.isFinal {
                    self.iosAVE.stop()
                    inputNode.removeTap(onBus: 0)
                    self.iosRequest = nil
                    self.iosTask = nil

                    self.textView.text = result.bestTranscription.formattedString
                }
            }else if error != nil{
                self.iosAVE.stop()
                inputNode.removeTap(onBus: 0)
                self.iosRequest = nil
                self.iosTask = nil

                self.textView.text = error?.localizedDescription ?? "(NULL)"
            }
        }

        let recordingFormat = iosAVE.inputNode?.outputFormat(forBus: 0)

        inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
            self.iosRequest?.append(buffer)
        }

        iosAVE.prepare()
        do{
            try iosAVE.start()
        } catch { print("Error: Start Record") }
    }
 }

 func avStop(){
        iosTask?.finish()
        iosRequest?.endAudio()
 }
}

1 个答案:

答案 0 :(得分:1)

kAFAssistantErrorDomain 203 SFSpeechRecognizer无法检测到任何结果的时间。如果在{s>完成或取消之前将SFSpeechAudioBufferRecognitionRequest设置为nil,则是203错误出现。

要解决该问题,只需在取消初始化之前调用iosRequest?.cancel()

...} else if error != nil {
    self.iosAVE.stop()
    inputNode.removeTap(onBus: 0)
    self.iosRequest?.cancel()
    self.iosRequest = nil
    self.iosTask = nil

    self.textView.text = error?.localizedDescription ?? "(NULL)"
}...

关于 kAFAssistantErrorDomain 1700 ,直到现在我还不知道是什么原因引起的。