语音指令是AI时代必不可少的技能
目前在汽车导航、智能家电领域已经得到广泛应用。是未来编程的必备技术之一。
语音指令是打开视图、控制对象、控制无人机、机器人、智能装备等非常简便的方法。可以避免导航结构、视图层级等限制,快速达成用户预期的目的。
下面我们来看看Swift如何实现语音指令。
第1步: 配置info.plist
需要获利用户授权,才能使用话筒与语音识别
第2步: 获取用户授权
import Speech
func requestSpeechRecognitionAuthorization() {
SFSpeechRecognizer.requestAuthorization { authStatus in
switch authStatus {
case .authorized:
print("用户已授权语音识别")
case .denied:
print("用户拒绝了语音识别权限")
case .restricted:
print("语音识别受限")
case .notDetermined:
print("用户尚未做出选择")
@unknown default:
print("未知状态")
}
}
}
第3步: 定义Speech服务
import Foundation
import Speech
class SpeechRecognitionManager: ObservableObject {
let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "zh-CN")) // 使用中文语音识别
let audioEngine = AVAudioEngine()
var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
var recognitionTask: SFSpeechRecognitionTask
@Published var recognizedCommand = [String]()
func stopRecording() {
// 停止语音识别任务
recognitionTask?.cancel()
// 停止音频引擎
audioEngine.stop()
recognitionRequest?.endAudio()
}
func startRecording() throws{
var commands = [String]()
// 创建语音识别请求
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
// 准备语音识别任务
guard let recognitionRequest = recognitionRequest else {
fatalError("无法创建 SFSpeechAudioBufferRecognitionRequest 实例")
}
recognitionRequest.shouldReportPartialResults = true
// 启动语音识别任务
recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { result, error in
// 处理识别结果
if let result = result {
// 处理识别结果
let bestTranscription = result.bestTranscription
let words = bestTranscription.segments
if let lastWord = words.last {
let recognizedWord = lastWord.substring
// 检查识别结果是否已经获取过,如果没有则添加到 commands,并添加到 recognizedWords 中
if !commands.contains(recognizedWord) {
commands.append(recognizedWord)
}
}
if let command = commands.last {
self.recognizedCommand.append(command)
}
} else if let error = error {
print("识别错误: \(error)")
}
})
// 启动音频引擎
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
try audioSession.setMode(.measurement)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
let inputNode = audioEngine.inputNode
let recordingFormat = inputNode.inputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
recognitionRequest.append(buffer)
}
audioEngine.prepare()
try audioEngine.start()
}
}
第4步: 调用服务进行语音识别
struct SpeechRecognitionView: View {
@State private var recognizedText = ""
@StateObject private var speechRecognitionManager = SpeechRecognitionManager()
@State var isRecording = false
var body: some View {
VStack {
Button(action: {
isRecording.toggle()
if isRecording {
do {
try speechRecognitionManager.startRecording()
recognizedText = speechRecognitionManager.recognizedCommand
} catch {
print("启动语音识别失败: \(error)")
}
} else {
speechRecognitionManager.stopRecording()
}
}) {
VStack {
Text("开始听写")
}
}
Text("\(recognizedText)")
}
}
}
最终实现效果如下:
通过语音指令,实现程序控制,是不是很爽,赶紧用起来吧。