Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Speech recognition has become a crucial feature in modern applications, enabling hands-free control and accessibility improvements. Apple provides robust tools for integrating speech recognition into macOS applications via the Speech framework. This article will guide you through the process of implementing speech recognition in a macOS environment using Swift.
To use the speech recognition capabilities, you need to import the Speech framework. Open your ViewController.swift
file and add the following import statement at the top:
import Cocoa
import Speech
Before you can use speech recognition, you need to request authorization from the user. Add the following code to your viewDidLoad
method:
override func viewDidLoad() {
super.viewDidLoad()
SFSpeechRecognizer.requestAuthorization { authStatus in
switch authStatus {
case .authorized:
print("Speech recognition authorized")
case .denied:
print("Speech recognition authorization denied")
case .restricted:
print("Speech recognition restricted on this device")
case .notDetermined:
print("Speech recognition not determined")
@unknown default:
fatalError("Unknown authorization status")
}
}
}
Create a method to start speech recognition. Add the following code to your ViewController.swift
file:
func startSpeechRecognition() {
guard let recognizer = SFSpeechRecognizer(), recognizer.isAvailable else {
print("Speech recognition not available")
return
}
let request = SFSpeechAudioBufferRecognitionRequest()
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
recognizer.recognitionTask(with: request) { result, error in
if let result = result {
print("Recognized speech: \(result.bestTranscription.formattedString)")
} else if let error = error {
print("Error recognizing speech: \(error.localizedDescription)")
}
}
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
request.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("Error starting audio engine: \(error.localizedDescription)")
}
}
You can trigger speech recognition by calling the startSpeechRecognition
method. For example, you might call this method when a button is pressed:
@IBAction func startButtonPressed(_ sender: NSButton) {
startSpeechRecognition()
}
Here is a complete example that includes a simple UI with a button to start speech recognition:
import Cocoa
import Speech
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
SFSpeechRecognizer.requestAuthorization { authStatus in
switch authStatus {
case .authorized:
print("Speech recognition authorized")
case .denied:
print("Speech recognition authorization denied")
case .restricted:
print("Speech recognition restricted on this device")
case .notDetermined:
print("Speech recognition not determined")
@unknown default:
fatalError("Unknown authorization status")
}
}
}
@IBAction func startButtonPressed(_ sender: NSButton) {
startSpeechRecognition()
}
func startSpeechRecognition() {
guard let recognizer = SFSpeechRecognizer(), recognizer.isAvailable else {
print("Speech recognition not available")
return
}
let request = SFSpeechAudioBufferRecognitionRequest()
let audioEngine = AVAudioEngine()
let inputNode = audioEngine.inputNode
recognizer.recognitionTask(with: request) { result, error in
if let result = result {
print("Recognized speech: \(result.bestTranscription.formattedString)")
} else if let error = error {
print("Error recognizing speech: \(error.localizedDescription)")
}
}
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
request.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("Error starting audio engine: \(error.localizedDescription)")
}
}
}
By following these steps, you can integrate speech recognition into your macOS applications using Apple's Speech framework. This feature can significantly enhance user experience by providing hands-free interaction and accessibility.