AVSpeechSynthesizer不工作一次使用后 [英] AVSpeechSynthesizer is not working After use one time

查看:446
本文介绍了AVSpeechSynthesizer不工作一次使用后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVSpeechSynthesizer发出一些文本数据的声音,它将首次使用,但此后将无法使用.

I'm using AVSpeechSynthesizer for voice out some text data, it'll work for first time but it didn't work after that.

我收到以下消息.

错误:-

AppName [859:101035] [AXTTSCommon]无法启动音频队列alp! 2018-10-26 18:06:53.253536 + 0530 AppName [859:101035] [AXTTSCommon] _BeginSpeaking:无法开始播放

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来执行此类任务.这是我的工作代码.希望这可以帮助.

You need to set the AVAudioSession for performing such tasks. Here is my working code. Hope this helps.

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.")
    }
}

这篇关于AVSpeechSynthesizer不工作一次使用后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆