Ios 根据swift中的avspeechoutrance更改滑块值

Ios 根据swift中的avspeechoutrance更改滑块值,ios,swift,avspeechsynthesizer,Ios,Swift,Avspeechsynthesizer,我正在使用avspeechoutrance读取文本,并尝试根据文本读取实现一个更改滑块 我试过:- myUtterance = AVSpeechUtterance(string: idFileText) myUtterance.rate = 0.3 synth.speak(myUtterance) synth.continueSpeaking() if let intValue = Int(idFileText) { self.sliderValue.setValue(Float(

我正在使用
avspeechoutrance
读取文本,并尝试根据文本读取实现一个更改滑块

我试过:-

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)
}
文本阅读器运行良好,但滑块值不变。

任何人都可以建议我在文本阅读器上实现滑动条的正确方法。

使用
AVSpeechSynthesizer
的代理
speechSynthesizer(uu2;:willSpeakRangeOfSpeechString:outreace:)

它提供正在讲话的当前字符范围。
您可以使用它来确定滑块位置,因为您应该已经知道整个文本的长度

一个基本的例子是:

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)
}
对于上述逻辑,请确保滑块对象的最小-最大范围为0-1。
当然,您需要对其进行改进以获得更顺利的进展


别忘了:

synth.delegate = self

这个滑块的用途是什么?就像音乐一样,播放和我试图在文本阅读器中实现的一样感谢它的工作。你能建议我,如果我有音频和文本都在那里,那么我怎么能改变slider@AnuragMishra将需要更多的细节来理解您的案例,但主要是正确的案例处理问题。let urlstring=idAudio,,let url=NSURL(string:urlstring),avPlayerItem=avPlayerItem.init(url:url!as url),,avPlayer=avPlayer.init(playerItem:avPlayerItem)avPlayer?.play()avPlayer?.addPeriodicTimeObserver(forInterval:CMTimeMake(1,30),queue:.main){time in let fraction=CMTimeGetSeconds(time)/CMTimeGetSeconds((self.avPlayer?.currentItem!.duration)!)self.sliderValue.value=Float(分数)}这是我的音频编码如何合并音频和文本reader@AnuragMishra我觉得很好。有什么问题吗?我想将音频和文本读取器合并为一个滑块值