ios语音识别错误域= kAFAssistantErrorDomain代码= 216“(null)”

时间:2017-06-26 19:30:24

标签: ios swift swift3 speech-recognition ios10

基本上我正在学习本教程之后的ios语音识别模块: https://medium.com/ios-os-x-development/speech-recognition-with-swift-in-ios-10-50d5f4e59c48

但是当我在iphone6上测试时,我总是遇到这个错误: 错误域= kAFAssistantErrorDomain代码= 216“(null)”

我在互联网上搜索过,但发现非常罕见的信息。

这是我的代码:

Replace(@"\u0026","&")

非常感谢。

4 个答案:

答案 0 :(得分:11)

我在遵循相同(优秀)教程时遇到了同样的问题,即使在GitHub上使用示例代码也是如此。要解决这个问题,我必须做两件事:

首先,在代码的开头添加request.endAudio()以停止在startButtonTapped操作中进行记录。这标志着录音的结束。我看到你已经在示例代码中完成了这项工作。

其次,在recordAndRecognizeSpeech函数中,当'recognitionTask'启动时,如果没有检测到语音,则'result'将为nil并且触发错误情况。所以,在尝试分配结果之前,我测试了result != nil

因此,这两个函数的代码如下所示: 1.更新了startButtonTapped:

@IBAction func startButtonTapped(_ sender: UIButton) {
    if isRecording {

        request.endAudio() // Added line to mark end of recording
        audioEngine.stop()

        if let node = audioEngine.inputNode {
            node.removeTap(onBus: 0)
        }
        recognitionTask?.cancel()

        isRecording = false
        startButton.backgroundColor = UIColor.gray

    } else {

        self.recordAndRecognizeSpeech()
        isRecording = true
        startButton.backgroundColor = UIColor.red
    }
}

并且2. recordAndRecognizeSpeechrecognitionTask = ...内的更新:

    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { (result, error) in
        if result != nil { // check to see if result is empty (i.e. no speech found)
            if let result = result {
                let bestString = result.bestTranscription.formattedString
                self.detectedTextLabel.text = bestString

                var lastString: String = ""
                for segment in result.bestTranscription.segments {
                    let indexTo = bestString.index(bestString.startIndex, offsetBy: segment.substringRange.location)
                    lastString = bestString.substring(from: indexTo)
                }
                self.checkForColoursSaid(resultString: lastString)

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

    }) 

我希望能帮助你。

答案 1 :(得分:2)

嘿,我得到了同样的错误,但现在它的工作非常精细。这段代码对你有帮助:)。

import UIKit
import Speech

class SpeechVC: UIViewController {

@IBOutlet weak var slabel: UILabel!
@IBOutlet weak var sbutton: UIButton!

let audioEngine = AVAudioEngine()
let SpeechRecognizer : SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask:SFSpeechRecognitionTask?

var isRecording = false
override func viewDidLoad() {
    super.viewDidLoad()


    self.requestSpeechAuthorization()

    // Do any additional setup after loading the view, typically from a nib.
}
func recordAndRecognizeSpeech()
{
    guard let node = audioEngine.inputNode else { return }
    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: { result, error in

        if let result = result
        {
            let bestString = result.bestTranscription.formattedString
            self.slabel.text = bestString

            var lastString : String = ""
            for segment in result.bestTranscription.segments
            {
                let indexTo = bestString.index(bestString.startIndex, offsetBy: segment.substringRange.location)
                lastString = bestString.substring(from: indexTo)
            }

        }else if let error = error
        {
            print(error)
        }
    })
}


@IBAction func startAction(_ sender: Any) {
    if isRecording == true
    {
        audioEngine.stop()
        recognitionTask?.cancel()
        isRecording = false
        sbutton.backgroundColor = UIColor.gray
    }
    else{
        self.recordAndRecognizeSpeech()
        isRecording = true
        sbutton.backgroundColor = UIColor.red
    }

}
func cancelRecording()
{
    audioEngine.stop()
    if let node = audioEngine.inputNode
    {
        audioEngine.inputNode?.removeTap(onBus: 0)
    }
    recognitionTask?.cancel()

}


func requestSpeechAuthorization()
{
    SFSpeechRecognizer.requestAuthorization { authStatus in
        OperationQueue.main.addOperation {
            switch authStatus
            {
            case .authorized :
                self.sbutton.isEnabled = true
            case .denied :
                self.sbutton.isEnabled = false
                self.slabel.text = "User denied access to speech recognition"
            case .restricted :
                self.sbutton.isEnabled = false
                self.slabel.text = "Speech Recognition is restricted on this Device"
            case .notDetermined :
                self.sbutton.isEnabled = false
                self.slabel.text = "Speech Recognition not yet authorized"
            }
        }

    }
}
}

答案 2 :(得分:1)

这将防止两个错误:上面提到的 Code = 216 'SFSpeechAudioBufferRecognitionRequest无法重复使用'错误。

  1. 使用完成而不是使用取消

  2. 停止识别
  3. 停止音频

像这样:

    // stop recognition
    recognitionTask?.finish()
    recognitionTask = nil

    // stop audio
    request.endAudio()
    audioEngine.stop()
    audioEngine.inputNode.removeTap(onBus: 0) // Remove tap on bus when stopping recording.

P.S。 audioEngine.inputNode似乎不再是可选值,因此,如果让我们构造,则不使用它。

答案 3 :(得分:0)

我遇到此错误是因为我正在模拟器上运行该应用程序。在常规设备上运行即可解决该问题。