简体   繁体   English

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

[英]Custom Keyboard Speech to text issue in IOS/Swift

I am trying to build custom keyboard with speech to text feature using Speech Framework but I am facing issue in audioengine.start() It always failed in device but it is working on simulator fine.我正在尝试使用Speech Framework构建具有语音到文本功能的自定义键盘,但我在audioengine.start()audioengine.start()问题它在设备中总是失败,但它在模拟器上运行良好。

Here is error:这是错误:

[AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): error 561145187 audioEngine couldn't start because of an error. [AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): error 561145187 audioEngine 由于错误而无法启动。 The operation couldn't be completed.操作无法完成。 (com.apple.coreaudio.avfaudio error 561145187.) (com.apple.coreaudio.avfaudio 错误 561145187。)

Can you please cross check your code with below code.你能用下面的代码交叉检查你的代码吗?

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()
                                                            }
    })

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM