ios - Swift 中没有数据传递时的补全块语法
问题描述
这很简单,但我无法让它工作。
我想在电话说话之前停止录音。没有数据被传递。
let words = "Hello world"
let utt = AVSpeechUtterance(string:words)
stopRecordingWithCompletion() {
voice.speak(utt)
}
func stopRecordinWithCompletion(closure: () -> Void) {
recognitionRequest?.endAudio()
recognitionRequest = nil
recognitionTask?.cancel()
recognitionTask = nil
let inputNode = audioEngine.inputNode
let bus = 0
inputNode?.removeTap(onBus: bus)
self.audioEngine.stop()
closure()
}
我究竟做错了什么?
解决方案
您当前的方法对此并不理想。
首先,AVSpeechSynthesizer提供了一个委托,您可以监控更改,包括它何时要说话。
speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
只需观察这一点,然后调用您的停止功能。不需要闭包,因为它是一个同步函数调用。
总之:
- 符合
AVSpeechSynthesizerDelegate
- 实施
speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
- 当上面的函数被调用时,让它调用你的
stopRecording()
函数
委托设置示例:
extension YourClassHere: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange,
utterance: AVSpeechUtterance) {
stopRecording()
}
}
推荐阅读
- regex - 如果有特定前缀,如何忽略正则表达式匹配
- amazon-web-services - 如何在 AWS 中将多个 EC2 实例连接在一起?
- kubernetes - Kubernetes pod 通过 Ubuntu OS 上的 MicrosK8s 在 Google Cloud 上的 GCE VM 上运行
- excel - 如何在oracle数据库表中存储双冒号值
- enterprise-architect - 如何从版本控制包中导出模型分支
- javascript - 如何减少javascript中的多个分支条件
- java - 带有 RowSorter 的 JTable 在过滤器更改时更改选择
- javascript - FilteringVue:设置默认值时不过滤列表
- youtube - 使用 PubSubHubbub (youtube API) 时有哪些限制?
- plsql - Delmsg 没有为 plsql 块填充