IOS 15 Swift 5我想完全转录录制的音频从文件
func transcribeAudio(url: URL) {
let recognizer = SFSpeechRecognizer()
let request = SFSpeechURLRecognitionRequest(url: url)
request.requiresOnDeviceRecognition = true;
request.taskHint = .dictation;
recognizer?.recognitionTask(with: request) { [unowned self] (result, error) in
guard let result = result else {
print("There was an error: (error!)")
return
}
if result.isFinal {
print(result.bestTranscription.formattedString)
}
}
}
控制台输出第一个短语而不是全文
如何重写代码来转录整个音频文件?谢谢!
解决方案是
- 把请求和识别器放到类props
- 排除部分结果
- 打印所有结果,而不是最终结果
func transcribeAudio(url: URL) {
request = SFSpeechURLRecognitionRequest(url: url)
request?.requiresOnDeviceRecognition = true;
request?.shouldReportPartialResults = false;
recognizer?.supportsOnDeviceRecognition = true;
recognizer?.recognitionTask(with: request!) { [unowned self] (result, error) in
guard let result = result else {
print("There was an error: (error!)")
return
}
RNEventEmitter.emitter.sendEvent(withName: "OnRecognition", body: result.bestTranscription.formattedString)
print(result.bestTranscription.formattedString)
}
}