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
import Speech | |
import UIKit | |
public class ViewController: UIViewController, SFSpeechRecognizerDelegate {} |
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 askForSpeechRecognitionPermissions() { | |
SFSpeechRecognizer.requestAuthorization { authStatus in | |
var enabled: Bool = false | |
var message: String? | |
switch authStatus { | |
case .authorized: | |
enabled = true |
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 var speechRecognizer: SFSpeechRecognizer! | |
private var recognitionTask: SFSpeechRecognitionTask? |
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
speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))! | |
speechRecognizer.delegate = self |
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
public func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) { | |
if available { | |
// e.g. enable record button | |
} else { | |
// e.g. disable record button | |
} | |
} |
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 var recognitionRequest: SFSpeechAudioBufferRecognitionRequest? | |
private let audioEngine = AVAudioEngine() |
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 { | |
// 1 | |
if let recognitionTask = self.recognitionTask { | |
recognitionTask.cancel() | |
self.recognitionTask = nil | |
} | |
// 2 | |
let audioSession = AVAudioSession.sharedInstance() | |
try audioSession.setCategory(AVAudioSessionCategoryRecord) |
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
@IBAction func recordButtonTapped() { | |
if audioEngine.isRunning { | |
audioEngine.stop() | |
recognitionRequest?.endAudio() | |
recordButton.isEnabled = false | |
} else { | |
try! startRecording() | |
} | |
} |
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 var recognitionRequest: SFSpeechURLRecognitionRequest? |
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) |
OlderNewer