Created
February 8, 2018 05:14
-
-
Save SunXiaoShan/3fcd5355879829bc8e143c9cde6d7875 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
// Tells the delegate when the recognition of all requested utterances is finished. | |
// @see https://developer.apple.com/reference/speech/sfspeechrecognitiontaskdelegate/1649215-speechrecognitiontask | |
open func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishSuccessfully successfully: Bool) { | |
resetSRMethod() | |
} | |
func resetSRMethod() { | |
stopNoAudioDurationTimer() | |
stopTimer() | |
InterruptEvent() | |
chickenStatus = .ChickenReady | |
recordButtonTapped() | |
} | |
fileprivate func recordButtonTapped() { | |
var ret = "" | |
self.localeIdentifier = locale | |
if audioEngine.isRunning { | |
audioEngine.stop() | |
recognitionRequest?.endAudio() | |
ret = self.recognizedText | |
let inputNode = self.getInputNode() | |
inputNode.removeTap(onBus: 0) | |
self.stopTimer() | |
} else { | |
self.delegate?.ChickenVRManagerStart(self) | |
self.recognizedText = "" | |
try! startRecording() | |
self.startTimer() | |
ret = "recognizeNow" | |
} | |
return ret | |
} | |
func startTimer() { | |
recognitionLimiter = Timer.scheduledTimer( | |
timeInterval: TimeInterval(self.recognitionLimitSec), | |
target: self, | |
selector:#selector(InterruptEvent), | |
userInfo: nil, | |
repeats: false | |
) | |
} | |
@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