SFSpeechAudioBufferRecognitionRequest无法重新使用

时间:2019-01-28 05:24:04

标签: swift

在此tutorial中使用了漂亮的代码!经过一些更正。语音识别代码正在运行。但是,如果我多次触发识别代码,则会弹出标题错误。很难找到解决此问题的文档。有人吗?

private 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 {
            self.sendAlert(message: "There has been an audio engine error.")
            return print (error)
        }

        guard let myRecognizer = SFSpeechRecognizer() else
        {
            self.sendAlert(message: "Speech recognition is not supported for your current locale.")
            return
        }

        if !myRecognizer.isAvailable
        {
            self.sendAlert(message: "Speech recognition is not currently available. Check back at a later time.")
            return
        }

        recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler:
        { result, error in
            if result != nil
            {
                if let result = result
                {
                    let bestString = result.bestTranscription.formattedString
                    self.detectedTextLabel.text = bestString
                }

                else if let error = error
                {
                    self.sendAlert(message: "There has been a speech recognition error.")
                    print(error)
                }
            }
        })
    }

以下是启动和停止识别器的功能。

    /// This button is the toggle for Starting and Stopping the Speech Recognition function
    @objc func didTapSpeechButton()
    {
        if isRecording == true {
            print("--> Stop Recording.")
            request.endAudio()  // Mark end of recording
            audioEngine.stop()
            let node = audioEngine.inputNode
            node.removeTap(onBus: 0)
            recognitionTask?.cancel()
            isRecording = false
            speechButton.backgroundColor = UIColor.red
        } else {
            print("--> Start Recording.")
            self.recordAndRecognizeSpeech()
            isRecording = true
            speechButton.backgroundColor = UIColor.gray
        }
    }

3 个答案:

答案 0 :(得分:0)

找到了一个可能的答案,因此决定分享。其他人可能会发现它很有用。这已添加到函数recordAndRecognizeSpeech()。

错误很明显,但解决方案对我们的学习而言并非如此。应用未崩溃。如果存在更好的答案-希望有人可以提供帮助。

// This resets the recognitionRequest so "...cannot be re-use..." error is avoided. 
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()   // recreates recognitionRequest object.
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }

答案 1 :(得分:0)

此错误与 Error Domain = kAFAssistantErrorDomain Code = 216“(null)” 有关。

必须使用完成而不是取消识别。

    // stop recognition
    recognitionTask?.finish()

找到我的完整答案here

答案 2 :(得分:0)

只需在想要停止或取消的地方添加此代码,Swift 4.2。

View -> Active Editor -> Soft-Wrap