Last active
February 8, 2018 05:09
-
-
Save SunXiaoShan/7b96846e3900a3e1f087e3afb3aeedcd to your computer and use it in GitHub Desktop.
VoiceRecognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didHypothesizeTranscription transcription: SFTranscription) { | |
self.recognizedText = transcription.formattedString | |
// Start judgment of silent time | |
self.stopNoAudioDurationTimer() | |
self.startNoAudioDurationTimer() | |
} | |
func startNoAudioDurationTimer() { | |
self.stopTimer() | |
noAudioDurationTimer = Timer.scheduledTimer( | |
timeInterval: TimeInterval(self.noAudioDurationLimitSec), | |
target: self, | |
selector:#selector(InterruptEvent), | |
userInfo: nil, | |
repeats: false | |
) | |
} | |
func stopNoAudioDurationTimer() { | |
if noAudioDurationTimer != nil { | |
noAudioDurationTimer?.invalidate() | |
noAudioDurationTimer = nil | |
} | |
} | |
@objc func InterruptEvent() { | |
var ret = "" | |
if audioEngine.isRunning { | |
audioEngine.stop() | |
recognitionRequest?.endAudio() | |
ret = self.recognizedText | |
} | |
let inputNode = self.getInputNode() | |
inputNode.removeTap(onBus: 0) | |
self.recognitionRequest = nil | |
self.recognitionTask = nil | |
recognitionLimiter = nil | |
noAudioDurationTimer = nil | |
self.resetAVAudioSession() | |
delegate?.ChickenVRManagerTimeout(self, ret) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment