Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFSpeechRecognizer kAFAssistantErrorDomain Code=203 "Retry"

I use SFSpeechRecognizer, basically to work.

1.But sometimes the following error occurs. And mostly before I did not execute avStop().

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

2.And completely unable to work in the background, will produce the following error.

[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()
 }
}
like image 431
user9186082 Avatar asked Jan 08 '18 03:01

user9186082


1 Answers

The kAFAssistantErrorDomain 203 is when the SFSpeechRecognizer could not detect any result when you finish or cancel a SFSpeechRecognitionTask. Maybe you are calling avStart(_) twice, causing to cancel the first task with no results.

About the kAFAssistantErrorDomain 1700 until now I do not know what cause the problem. But only happened to me with a jailbroken iPhone.

like image 173
Ángel Téllez Avatar answered Oct 23 '22 04:10

Ángel Téllez