Можете ли вы перепроверить код с приведенным ниже кодом.
let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?
func recordAndRecognizeSpeech() {
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.removeTap(onBus: 0)
if recordingFormat.sampleRate > 0 {
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print(error)
}
guard let myRecognizer = SFSpeechRecognizer() else {
// Speech Recognizer is not supported
return
}
if !myRecognizer.isAvailable {
// Recognizer is not available right now
return
}
request.shouldReportPartialResults = true
// Create recognition task for given recognition request and retruns the result string on success
recognitionTask = speechRecognizer?.recognitionTask(with: request,
resultHandler: { (result, error) in
var isFinal = false
if result != nil {
var spokenWord = (result?.bestTranscription.formattedString ?? "")
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {
self.audioEngine.stop()
}
})
}