作者:mobiledu2502889217 | 来源:互联网 | 2022-12-21 16:09
我有一个应用程序,我们需要打开基于语音命令的某些屏幕,如果用户说"打开设置"然后它应该打开设置屏幕,到目前为止,我已经使用了SpeechKit
框架,但我无法检测到的结束言语沉默.就像Siri一样.我想检测用户是否已结束他的句子/短语.
请在下面的代码中找到我SpeechKit
用两种方式集成框架的代码.
A)通过闭包(recognitionTask(with request: SFSpeechRecognitionRequest, resultHandler: @escaping (SFSpeechRecognitionResult?, Error?) -> Swift.Void) -> SFSpeechRecognitionTask
)
let audioEngine = AVAudioEngine()
let speechRecognizer = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?
func startRecording() throws {
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024,
format: recordingFormat) { [unowned self]
(buffer, _) in
self.request.append(buffer)
}
audioEngine.prepare()
try audioEngine.start()
weak var weakSelf = self
recognitiOnTask= speechRecognizer?.recognitionTask(with: request) {
(result, error) in
if result != nil {
if let transcription = result?.bestTranscription {
weakSelf?.idenifyVoiceCommand(transcription)
}
}
}
}
但是当我说任何单词/句子如"打开设置"然后recognitionTask(with:)
多次调用closure()并且我将方法(idenifyVoiceCommand
)放在多次调用的闭包中时,我怎样才能限制只调用一次.
我还在谷歌上搜索定时器逻辑(SFSpeechRecognizer - 检测话语的结束),但在我的观点中它不起作用,因为我没有停止音频引擎,因为它像Siri一样持续听用户的声音.
B)通过委托(SFSpeechRecognitionTaskDelegate
)
speechRecognizer.recognitionTask(with:self.request,delegate:self)
func speechRecognitionTaskWasCancelled(_ task: SFSpeechRecognitionTask) {
}
func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishSuccessfully successfully: Bool) {
}
我发现在发言结束时处理的代表不会调用它,有时候不小心调用它.