Created
September 25, 2016 22:47
-
-
Save gaelfoppolo/130852f8eebadb092bdaf6cc5d352bb6 to your computer and use it in GitHub Desktop.
This file contains 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
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