Skip to content

Instantly share code, notes, and snippets.

@gaelfoppolo
Created September 25, 2016 22:47
Show Gist options
  • Save gaelfoppolo/130852f8eebadb092bdaf6cc5d352bb6 to your computer and use it in GitHub Desktop.
Save gaelfoppolo/130852f8eebadb092bdaf6cc5d352bb6 to your computer and use it in GitHub Desktop.
private func startRecording() throws {
if let recognitionTask = recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
let path = Bundle.main.path(forResource: "your-file", ofType: "mp3")
if let path = path {
let url = URL(fileURLWithPath: path)
recognitionRequest = SFSpeechURLRecognitionRequest(url: url)
}
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechURLRecognitionRequest object") }
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
var isFinal = false
if let result = result {
self.textView.text = result.bestTranscription.formattedString
isFinal = result.isFinal
}
if error != nil || isFinal {
self.recognitionRequest = nil
self.recognitionTask = nil
self.recordButton.isEnabled = true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment