AVSpeechSynthesizer 使用一次后无法正常工作
AVSpeechSynthesizer is not working After use one time
我正在使用 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)
}
试试下面的代码:
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.")
}
对我来说,问题是我的 speaker volume
关闭了,它也在 vibration mode
我正在使用 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)
}
试试下面的代码:
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.")
}
对我来说,问题是我的 speaker volume
关闭了,它也在 vibration mode