IOS/Swift/AVSpeechSynthesizer:控制入队话语的速度
IOS/Swift/AVSpeechSynthesizer: Control Speed of Enqueued Utterances
为了本着 this tutorial 的精神对有声读物的语音施加更大的控制,尽管我没有完全遵循它,但我尝试发送更小的字符串片段,例如单独的块中的短语。语音合成器将每个话语排入队列并一个接一个地说出来。从理论上讲,这应该可以让您更好地控制语音听起来不那么机械化。
我可以让合成器按顺序说出这些块,但是每个块之间有很长的延迟,所以这听起来比同时发送所有文本要糟糕得多。
有没有办法加快排队速度,让话语一个接一个地讲出来,没有延迟?
将属性:utt.preUtteranceDelay 和 utt.postUtteranceDelay 设置为零秒似乎没有任何效果
这是我的代码:
phraseCounter = 0
func readParagraph(test: String) {
let phrases = test.components(separatedBy: " ")
for phrase in phrases {
phraseCounter = phraseCounter+1
let utt = AVSpeechUtterance(string:phrase)
let preUtteranceDelayInSecond = 0
let postUtteranceDelayInSecond = 0
utt.preUtteranceDelay = TimeInterval.init(exactly:preUtteranceDelayInSecond)!
utt.postUtteranceDelay = TimeInterval.init(exactly:postUtteranceDelayInSecond)!
voice.delegate = self
if (phraseCounter == 2) {
utt.rate = .8
}
voice.speak(utt)
}
}
Is there anyway to speed up the queue so that the utterances are spoken one after the other with no delay?
正如您所做的,唯一的方法是将 post 和 pre UtteranceDelay
属性设置为 0,顺便说一句,这是默认值。
按照建议 here,我在下文中实现了代码片段 (Xcode 10, Swift 5.0 和 iOS 12.3.1) 检查不同 UtteranceDelay
值的影响⟹ 0 是提高排队话语速度的最佳解决方案.
var synthesizer = AVSpeechSynthesizer()
var playQueue = [AVSpeechUtterance]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for i in 1...10 {
let stringNb = "Sentence number " + String(i) + " of the speech synthesizer."
let utterance = AVSpeechUtterance(string: stringNb)
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
utterance.pitchMultiplier = 1.0
utterance.volume = 1.0
utterance.postUtteranceDelay = 0.0
utterance.preUtteranceDelay = 0.0
playQueue.append(utterance)
}
synthesizer.delegate = self
for utterance in playQueue {
synthesizer.speak(utterance)
}
}
如果代码中的“0”值导致延迟太重要,传入的字符串可能是问题所在? (根据您的需要调整上面的代码片段)
为了本着 this tutorial 的精神对有声读物的语音施加更大的控制,尽管我没有完全遵循它,但我尝试发送更小的字符串片段,例如单独的块中的短语。语音合成器将每个话语排入队列并一个接一个地说出来。从理论上讲,这应该可以让您更好地控制语音听起来不那么机械化。
我可以让合成器按顺序说出这些块,但是每个块之间有很长的延迟,所以这听起来比同时发送所有文本要糟糕得多。
有没有办法加快排队速度,让话语一个接一个地讲出来,没有延迟?
将属性:utt.preUtteranceDelay 和 utt.postUtteranceDelay 设置为零秒似乎没有任何效果
这是我的代码:
phraseCounter = 0
func readParagraph(test: String) {
let phrases = test.components(separatedBy: " ")
for phrase in phrases {
phraseCounter = phraseCounter+1
let utt = AVSpeechUtterance(string:phrase)
let preUtteranceDelayInSecond = 0
let postUtteranceDelayInSecond = 0
utt.preUtteranceDelay = TimeInterval.init(exactly:preUtteranceDelayInSecond)!
utt.postUtteranceDelay = TimeInterval.init(exactly:postUtteranceDelayInSecond)!
voice.delegate = self
if (phraseCounter == 2) {
utt.rate = .8
}
voice.speak(utt)
}
}
Is there anyway to speed up the queue so that the utterances are spoken one after the other with no delay?
正如您所做的,唯一的方法是将 post 和 pre UtteranceDelay
属性设置为 0,顺便说一句,这是默认值。
按照建议 here,我在下文中实现了代码片段 (Xcode 10, Swift 5.0 和 iOS 12.3.1) 检查不同 UtteranceDelay
值的影响⟹ 0 是提高排队话语速度的最佳解决方案.
var synthesizer = AVSpeechSynthesizer()
var playQueue = [AVSpeechUtterance]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for i in 1...10 {
let stringNb = "Sentence number " + String(i) + " of the speech synthesizer."
let utterance = AVSpeechUtterance(string: stringNb)
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
utterance.pitchMultiplier = 1.0
utterance.volume = 1.0
utterance.postUtteranceDelay = 0.0
utterance.preUtteranceDelay = 0.0
playQueue.append(utterance)
}
synthesizer.delegate = self
for utterance in playQueue {
synthesizer.speak(utterance)
}
}
如果代码中的“0”值导致延迟太重要,传入的字符串可能是问题所在? (根据您的需要调整上面的代码片段)