Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVSpeechSynthesizer在使用一次后不工作_Ios_Swift_Text To Speech - Fatal编程技术网

Ios AVSpeechSynthesizer在使用一次后不工作

Ios AVSpeechSynthesizer在使用一次后不工作,ios,swift,text-to-speech,Ios,Swift,Text To Speech,我正在使用AVSpeechSynthesizer来输出一些文本数据,这是我第一次使用它,但之后就没用了 我收到下面的消息 错误:- AppName[859:101035][AXTTSCommon]启动音频队列alp失败! 2018-10-26 18:06:53.253536+0530应用程序名[859:101035][AXTTSCommon] _BeginSpeaking:无法开始播放 下面我分享我的代码 let synth = AVSpeechSynthesizer() filepriv

我正在使用AVSpeechSynthesizer来输出一些文本数据,这是我第一次使用它,但之后就没用了

我收到下面的消息

错误:-

AppName[859:101035][AXTTSCommon]启动音频队列alp失败! 2018-10-26 18:06:53.253536+0530应用程序名[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)
 }
请尝试以下代码:

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

您需要设置执行此类任务的
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.")
    }
}
这对我有用:

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

对我来说,问题是我的
扬声器音量
已关闭,并且它也处于
振动模式

我尝试了此功能,但它不起作用。仍然得到相同的错误。我能在Objective-C中得到相同的代码吗???因为我也有同样的问题&我的代码在Objective-C中