gpt4 book ai didi

ios - AVSpeechSynthesizer 使用一次后无法正常工作

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:05 24 4
gpt4 key购买 nike

我正在使用 AVSpeechSynthesizer 为一些文本数据发声,它第一次可以工作,但之后就没用了。

我收到以下消息。

错误:-

AppName[859:101035] [AXTTSCommon] Failure starting audio queue alp! 2018-10-26 18:06:53.253536+0530 AppName[859:101035] [AXTTSCommon] _BeginSpeaking: couldn't begin playback

下面我分享我的代码。

 let synth = AVSpeechSynthesizer()

fileprivate func speakText(voiceOutdata: String )
{
let utterance = AVSpeechUtterance(string: voiceOutdata)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
synth.speak(utterance)
}

最佳答案

您需要设置AVAudioSession 来执行这些任务。这是我的工作代码。希望这可以帮助。

func speakText(voiceOutdata: String ) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: .default, options: .defaultToSpeaker)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}

let utterance = AVSpeechUtterance(string: voiceOutdata)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

let synth = AVSpeechSynthesizer()
synth.speak(utterance)

defer {
disableAVSession()
}
}

private func disableAVSession() {
do {
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't disable.")
}
}

关于ios - AVSpeechSynthesizer 使用一次后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53009032/

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