Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. @IBAction func recordingButtonTapped(_ sender: AnyObject) {
  2. if audioEngine.isRunning {
  3. audioEngine.stop()
  4. recognitionRequest.endAudio()
  5. recognitionTask.cancel()
  6. recordingButton.setTitle("Start Recording", for: [])
  7. } else {
  8. recordingButton.setTitle("Stop Recording", for: [])
  9. do {
  10.  
  11. let audioSession = AVAudioSession.sharedInstance()
  12. try audioSession.setCategory(AVAudioSessionCategoryRecord)
  13. try audioSession.setMode(AVAudioSessionModeMeasurement)
  14. try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
  15. if let inputNode = audioEngine.inputNode {
  16. recognitionRequest.shouldReportPartialResults = true
  17. recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
  18. if let result = result {
  19. self.textView.text = result.bestTranscription.formattedString
  20. if result.isFinal {
  21. self.audioEngine.stop()
  22. inputNode.removeTap(onBus: 0)
  23. self.recordingButton.setTitle("Start Recording", for: [])
  24. }
  25. }
  26. })
  27.  
  28. let recordingFormat = inputNode.outputFormat(forBus: 0)
  29. inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat, block: { (buffer, when) in
  30. self.recognitionRequest.append(buffer)
  31. })
  32. audioEngine.prepare()
  33. try audioEngine.start()
  34. }
  35. } catch {
  36. // Handle errors
  37. }
  38. }//end of else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement