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()
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With