2

SFSpeechRecognizer 在 IOS 中运行良好,但在 IPAD 中不可用。我不知道如何解决它。请给我一个解决方案。

func recordAndRecognizeSpeech() {
        
        let node = audioEngine.inputNode
        let recordingFormat = node.outputFormat(forBus: 0)
        node.installTap(onBus: 0, bufferSize: 0, format: recordingFormat) { buffer, _ in
            self.request.append(buffer)
        }
        audioEngine.prepare()
        do {
            try audioEngine.start()
        } catch {
            return print(error)
        }
        guard let myRecognizer = SFSpeechRecognizer() else {
            // A recognizer is not supported for the current locale
            return
        }
        if !myRecognizer.isAvailable {
            print("Speech recognition not available")
            return
        }
        isFinal = false

        recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler:  { result, error in
            if let result = result {
                self.recognizedSpeechTextView.text = result.bestTranscription.formattedString
                self.qnaTextField.text = result.bestTranscription.formattedString
                self.textFieldDidChange(self.qnaTextField)

            } else if let error = error {
                print(error)
            }
            
            if !self.isFinal {
                self.startSpeechTimer()
            }
        })
    }

在 iPad 调用函数中 if !myRecognizer.isAvailable { print("语音识别不可用") return }

4

1 回答 1

0

SFSpeechRecognizer.isAvailable 可能返回 false 的至少两个可能原因存在:

于 2021-10-07T18:04:17.263 回答