首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能重复使用SFSpeechAudioBufferRecognitionRequest。

不能重复使用SFSpeechAudioBufferRecognitionRequest。
EN

Stack Overflow用户
提问于 2019-01-28 05:24:03
回答 3查看 2.8K关注 0票数 7

在这个教程中使用了漂亮的代码!做些修正。语音识别代码正在工作。但是,如果我触发识别代码超过两次,标题中的错误就会弹出。很难找到解决这个问题的文档。有没有人?

代码语言:javascript
运行
复制
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)
                }
            }
        })
    }

下面是启动和停止识别器的函数。

代码语言:javascript
运行
复制
    /// 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
        }
    }
EN

Stack Overflow用户

发布于 2019-03-18 11:32:47

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

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

代码语言:javascript
运行
复制
    // stop recognition
    recognitionTask?.finish()

找到我的完整答案,这里

票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54395966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档