IOS/Swift 中的自定义键盘语音转文本问题



我正在尝试使用语音框架构建具有语音转文本功能的自定义键盘,但我在audioengine.start()中遇到了问题 它总是在设备中失败,但它在模拟器上工作正常。

这是错误:

[AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0((: 错误 561145187 audioEngine 无法启动 因为错误。无法完成操作。 (com.apple.coreaudio.avfaudio error 561145187.(

你能用下面的代码交叉检查你的代码吗?

let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?
func recordAndRecognizeSpeech() {
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.removeTap(onBus: 0)
if recordingFormat.sampleRate > 0 {
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print(error)
}
guard let myRecognizer = SFSpeechRecognizer() else {
// Speech Recognizer is not supported
return
}
if !myRecognizer.isAvailable {
// Recognizer is not available right now
return
}

request.shouldReportPartialResults = true
// Create recognition task for given recognition request and retruns the result string on success
recognitionTask = speechRecognizer?.recognitionTask(with: request,
resultHandler: { (result, error) in
var isFinal = false
if result != nil {
var spokenWord = (result?.bestTranscription.formattedString ?? "")
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {
                                                       self.audioEngine.stop()
}
})
}

最新更新