用户说完后停止语音识别 [英] Stop speech recognition when user is finished speaking

查看:107
本文介绍了用户说完后停止语音识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Siri如何能够确定我说完的时间。我想知道的原因是我想用我的应用程序与Apple的语音识别API实现类似的功能。这是可行的,还是通过用户输入知道用户何时停止说话的唯一方法?

How is Siri is able to determine when I'm finished speaking. The reason I would like to know is that I would like to implement similar functionality with Apple's Speech Recognition API with my app. Is this doable, or is the only way to know when the user has stopped speaking is via user input?

推荐答案

你可以使用一个计时器,我有同样的问题,我无法用优雅的方法解决它。

You can use a timer, i had the same problem and I could not solve it with an elegant method.

fileprivate var timer:Timer?
func startRecordingTimer() {
    lastString = ""
    createTimerTimer(4)
}
func stopRecordingTimer() {
    timer?.invalidate()
    timer = nil
}
fileprivate func whileRecordingTimer() {
    createTimerTimer(2)
}
fileprivate var lastString = ""
func createTimerTimer(_ interval:Double) {
    OperationQueue.main.addOperation({[unowned self] in
        self.timer?.invalidate()
        self.timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { (_) in
            self.timer?.invalidate()
            if(self.lastString.characters.count > 0){
                //DO SOMETHING
            }else{
                self.whileRecordingTimer()
            }
        }
    })
}

SFSpeechRecognitionTaskDelegate

public func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didHypothesizeTranscription transcription: SFTranscription) {
    let result = transcription.formattedString
    lastString = result
}

这篇关于用户说完后停止语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆