用户说完后停止语音识别

14 speech-recognition ios swift

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

Sci*_*nfu 4

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

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()
            }
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

并在SFSpeechRecognitionTaskDelegate

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