首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据AVSpeechUtterance更改滑块值

根据AVSpeechUtterance更改滑块值
EN

Stack Overflow用户
提问于 2018-04-03 05:40:36
回答 1查看 427关注 0票数 1

我正在使用AVSpeechUtterance来读取文本,并尝试根据文本读取实现更改滑块拇指。

我试过:-

代码语言:javascript
复制
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)
}

文本阅读器做得很好,但滑块值不变。

有谁能建议我在文本阅读器上实现滑块的正确方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-03 06:17:24

使用AVSpeechSynthesizer的委托speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)

它提供了当前正在说话的字符范围。

您可以使用它来确定滑块位置,因为您应该已经知道整个文本的长度。

一个基本的例子是:

代码语言:javascript
复制
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.

当然,你需要改进它才能取得更顺利的进展。

别忘了:

代码语言:javascript
复制
synth.delegate = self
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49622980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档