【问题标题】:AVSpeechSynthesizer is not working After use one timeAVSpeechSynthesizer 不工作 使用一次后
【发布时间】:2018-10-26 12:44:15
【问题描述】:

我正在使用 AVSpeechSynthesizer 语音输出一些文本数据,它会第一次工作,但之后就没有工作了。

我收到以下消息。

错误:-

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

下面我分享我的代码。

 let synth = AVSpeechSynthesizer()

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

【问题讨论】:

    标签: ios swift text-to-speech


    【解决方案1】:

    您需要设置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.")
        }
    }
    

    【讨论】:

    • 我可以在 Objective-C 中获得相同的代码吗?因为我也有同样的问题&我的代码在 Objective-C 中
    【解决方案2】:

    试试下面的代码:

    let synthesizer = AVSpeechSynthesizer()
     fileprivate func speakText(voiceOutdata: String ){
        synthesizer.stopSpeakingAtBoundary(.Immediate)
        let utterance = AVSpeechUtterance(string: voiceOutdata)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
        synthesizer.speakUtterance(utterance)
    }
    

    【讨论】:

    • 我试过了,但它不起作用。仍然出现同样的错误。
    【解决方案3】:

    它对我有用:

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

    【讨论】:

      【解决方案4】:

      对我来说,问题是我的speaker volume 关闭了,它也在vibration mode

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        相关资源
        最近更新 更多