gpt4 book ai didi

ios - Swift 中没有数据传递时的完成 block 语法

转载 作者:行者123 更新时间:2023-11-28 13:37:17 25 4
gpt4 key购买 nike

这太简单了,但我无法让它工作。

我想在电话说话之前停止录音。没有数据被传递。

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:)

只需观察这一点,然后调用停止函数。不需要关闭,因为它是 synchronous函数调用。

总结:

  1. 符合AVSpeechSynthesizerDelegate
  2. 实现speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
  3. 当上面的函数被调用时,让它调用你的stopRecording()函数

委托(delegate)设置示例:

extension YourClassHere: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange,
utterance: AVSpeechUtterance) {
stopRecording()
}
}

关于ios - Swift 中没有数据传递时的完成 block 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com