Stop speech recognition when user is finished speaking

做~自己de王妃 提交于 2019-12-03 11:47:13

问题


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?


回答1:


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()
            }
        }
    })
}

and in SFSpeechRecognitionTaskDelegate

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


来源:https://stackoverflow.com/questions/41421159/stop-speech-recognition-when-user-is-finished-speaking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!