7

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()
 }
}
user9186082
  • 71
  • 1
  • 4

1 Answers1

7

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.

Ángel Téllez
  • 987
  • 9
  • 13
  • Why was "finish it" struck out? I get this error even though I'm finishing the request. – Mr Rogers Jun 17 '19 at 19:08
  • 1
    My bad. The first answer I wrote includes code for cancel or finish the recognition task, but the original code had already a function with the `finish()`, so I edited it to only include the `cancel()`. Maybe I should rewrite it inside parenthesis. Even worse, I realize that my answer is bad, I mixed up variables and methods. I do not know what I was thinking. I am going to correct it. – Ángel Téllez Jun 17 '19 at 22:26
  • 1
    I've seen error 1700 on a legal phone. Not sure what causes it though. – mike nelson Nov 04 '19 at 11:41
  • This answer is a guess or is there any official documentation that backs it? – kikeenrique May 11 '20 at 12:10
  • Pure guess. But I do not know if there is an official documentation, at least I have not found it. – Ángel Téllez May 12 '20 at 17:19
  • It only happened to me in my jailbroken iPhones, but I remember reading that other people have the same error without a jailbreak, so I do not know yet what that error means :( – Ángel Téllez May 18 '20 at 23:55