[英]Completion block syntax when no data passed in 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:)
只要注意这一点,然后调用stop函数即可。 因为它是一个同步函数调用,所以不需要关闭。
综上所述:
AVSpeechSynthesizerDelegate
speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
stopRecording()
函数 委托设置的示例:
extension YourClassHere: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange,
utterance: AVSpeechUtterance) {
stopRecording()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.