我正在使用AVSpeechUtterance来读取文本,并尝试根据文本读取实现更改滑块拇指。
我试过:-
myUtterance = AVSpeechUtterance(string: idFileText)
myUtterance.rate = 0.3
synth.speak(myUtterance)
synth.continueSpeaking()
if let intValue = Int(idFileText) {
self.sliderValue.setValue(Float(intValue),
animated: true)
}文本阅读器做得很好,但滑块值不变。
有谁能建议我在文本阅读器上实现滑块的正确方法吗?
发布于 2018-04-03 06:17:24
使用AVSpeechSynthesizer的委托speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)。
它提供了当前正在说话的字符范围。
您可以使用它来确定滑块位置,因为您应该已经知道整个文本的长度。
一个基本的例子是:
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange,
utterance: AVSpeechUtterance) {
let progress = Float(characterRange.location + characterRange.length)
/ Float(utterance.speechString.count)
print(progress)
self.sliderValue.setValue(progress,
animated: true)
}对于上述逻辑,请确保滑块对象min-max范围为0-1.
当然,你需要改进它才能取得更顺利的进展。
别忘了:
synth.delegate = selfhttps://stackoverflow.com/questions/49622980
复制相似问题