gpt4 book ai didi

ios - speechRecognizer 不返回答案

转载 作者:行者123 更新时间:2023-11-28 14:21:34 24 4
gpt4 key购买 nike

我正在学习有关 iOS 10 语音识别 API 的一些教程 (https://code.tutsplus.com/tutorials/using-the-speech-recognition-api-in-ios-10--cms-28032?ec_unit=translation-info-language)我的版本不起作用。语音输入没有文本响应。我遵循了教程,但我不得不进行一些更改(显然,较新版本的 Swift 不接受与教程中完全相同的某些代码行)。你们能告诉我一些关于它如何以及为什么不起作用的想法吗?

这是我正在运行的方法:

func startRecording() {
// Setup audio engine and speech recognizer
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}

// Prepare and start recording
audioEngine.prepare()
do {
try audioEngine.start()
self.status = .recognizing
} catch {
return print(error)
}

// Analyze the speech
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
if let result = result {
self.tview.text = result.bestTranscription.formattedString
NSLog(result.bestTranscription.formattedString)
} else if let error = error {
print(error)
NSLog(error.localizedDescription)
}
})
}

调试时,speechRecognizer 和 recognitionTask 都没有 nil 值。

这就是我在 ViewController 上定义变量的方式:

let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?

工作设置:在 2017 iPad、iOS 11.4 上测试。Xcode 9.4.1、Swift 4.1。

谢谢!

最佳答案

这个问题是因为 AVAudioSession 没有设置为 Record。试试这个。

在 View Controller 中添加

let audioSession = AVAudioSession.sharedInstance()

您的 final方法将是。

func startRecording() {
//Change / Edit Start
do {
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}
//Change / Edit Finished
// Setup audio engine and speech recognizer
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}

// Prepare and start recording
audioEngine.prepare()
do {
try audioEngine.start()
self.status = .recognizing
} catch {
return print(error)
}

// Analyze the speech
recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
if let result = result {
self.tview.text = result.bestTranscription.formattedString
NSLog(result.bestTranscription.formattedString)
} else if let error = error {
print(error)
NSLog(error.localizedDescription)
}
})
}

关于ios - speechRecognizer 不返回答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com