Error Domain=kAFAssistantErrorDomain Code=216 "(null)"

Basically I am learning ios speech recognition module following this tutorial:https://medium.com/ios-os-x-development/speech-recognition-with-swift-in-ios-10-50d5f4e59c48


But when I test it on my iphone6, I always got this error: Error Domain=kAFAssistantErrorDomain Code=216 "(null)"


I searched it on the internet, but find very rare info about this.


Here is my code:


/
/
/
/
/
/
/
import UIKit
import AVFoundation
import Speech
class ViewController: UIViewController, SFSpeechRecognizerDelegate {
  
    /
    let audioEngine = AVAudioEngine()
    let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
    var request = SFSpeechAudioBufferRecognitionRequest()
    var recognitionTask: SFSpeechRecognitionTask?
    var isRecording = false
  
    override func viewDidLoad() {
        /
        /
        self.requestSpeechAuthorization()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
  
    /
    @IBOutlet weak var detectText: UILabel!
    @IBOutlet weak var startButton: UIButton!
  
    /
    @IBAction func startButtonTapped(_ sender: UIButton) {
        if isRecording == true {
          
          
            audioEngine.stop()
/
/
/
            audioEngine.inputNode?.removeTap(onBus: 0)
            /
            self.request.endAudio()
          
            /
            if let recognitionTask = recognitionTask {
                recognitionTask.cancel()
                self.recognitionTask = nil
            }
          
          
            /
            /
            isRecording = false
            startButton.backgroundColor = UIColor.gray
        } else {
            self.recordAndRecognizeSpeech()
            isRecording = true
            startButton.backgroundColor = UIColor.red
        }
    }
  
    /
    func showAlert(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
        DispatchQueue.main.async { [unowned self] in
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: handler))
            self.present(alertController, animated: true, completion: nil)
        }
    }
  
    /
    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 {
            self.showAlert(title: "SpeechNote", message: "There has been an audio engine error.", handler: nil)
            return print(error)
        }
        guard let myRecognizer = SFSpeechRecognizer() else {
            self.showAlert(title: "SpeechNote", message: "Speech recognition is not supported for your current locale.", handler: nil)
            return
        }
        if !myRecognizer.isAvailable {
            self.showAlert(title: "SpeechNote", message: "Speech recognition is not currently available. Check back at a later time.", handler: nil)
            /
            return
        }
        recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
            if let result = result {
              
                let bestString = result.bestTranscription.formattedString
                self.detectText.text = bestString
              
/
/
/
/
/
/
            } else if let error = error {
                self.showAlert(title: "SpeechNote", message: "There has been a speech recognition error.", handler: nil)
                print(error)
            }
        })
    }
  
    /
    func requestSpeechAuthorization() {
        SFSpeechRecognizer.requestAuthorization { authStatus in
            OperationQueue.main.addOperation {
                switch authStatus {
                case .authorized:
                    self.startButton.isEnabled = true
                case .denied:
                    self.startButton.isEnabled = false
                    self.detectText.text = "User denied access to speech recognition"
                case .restricted:
                    self.startButton.isEnabled = false
                    self.detectText.text = "Speech recognition restricted on this device"
                case .notDetermined:
                    self.startButton.isEnabled = false
                    self.detectText.text = "Speech recognition not yet authorized"
                }
            }
        }
    }
}

Replies

I have the same proble.

In july my codesworked well but after vacation season the same code doesn't work.

My speechRecognition request returns an error code (216)

How have you solved this problem?

I've found that I can safely ignore this error, and recognition will continue.