该按钮在SpeechKit / Swift中不起作用

时间:2019-02-25 12:55:02

标签: ios swift speechkit

您可以在下面找到我的代码。我写了代码,在那里我可以识别我的讲话并阻止它成为文本。但是不幸的是,这段代码仅工作一次,当我需要再次添加识别语音时,我应该重新启动应用程序或viewController。所以我有一个问题:如果每次我按一下按钮并调用该函数而不重新启动应用程序时都需要识别语音,该如何更改代码。

这是功能代码:

func recordAndRecognizeSpeech() {
    let node = audioEngine.inputNode
    let recordingFormat = node.outputFormat(forBus: 0)
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
        self.request.append(buffer)
    }
    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        return print(error)
    }
    guard let myRecognizer = SFSpeechRecognizer() else {
        return
    } 
    if !myRecognizer.isAvailable {
        return
    }
    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { [weak self] result, error in
        if let result = result {
            let bestString = result.bestTranscription.formattedString
            self?.recognizedLabel.text = bestString
        } else if let error = error {
            print(error)
        }
    })
}

这是按钮代码:

@IBAction func micBtnTouchDown(_ sender: Any) {
    self.recordAndRecognizeSpeech()
}

谢谢,祝你有美好的一天!

0 个答案:

没有答案
相关问题