我目前正在开发一个iOS应用程序,使用AVSynthesizer将文本转换为语音。
我想做的是,当合成器说话时,可以更改话语速率,并使用滑块更改说话速度。
我在滑块的 IBAction 中执行此操作:self.utterance = sender.value
但是合成器不会改变速度。我一直在寻找信息,但还没有找到东西。我做什么?提前谢谢。
好的,所以在玩了一下我不知道的这个很酷的功能之后,我找到了一种改变话语速率的方法。主要问题是语句当前由合成器排队,无法更改rate
。对应于文档:
/* Setting these values after a speech utterance has been enqueued will have no effect. */
open var rate: Float // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
open var pitchMultiplier: Float // [0.5 - 2] Default = 1
open var volume: Float // [0-1] Default = 1
因此,解决方法是停止合成器,并用修剪的字符串向他提供新的话语。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var synthesizer: AVSpeechSynthesizer!
var string: String!
var currentRange: NSRange = NSRange(location: 0, length: 0)
@IBAction func sl(_ sender: UISlider) {
synthesizer.stopSpeaking(at: .immediate)
if currentRange.length > 0 {
let startIndex = string.index(string.startIndex, offsetBy: NSMaxRange(currentRange))
let newString = string.substring(from: startIndex)
string = newString
synthesizer.speak(buildUtterance(for: sender.value, with: string))
}
}
func buildUtterance(for rate: Float, with str: String) -> AVSpeechUtterance {
let utterance = AVSpeechUtterance(string: str)
utterance.rate = rate
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
return utterance
}
override func viewDidLoad() {
super.viewDidLoad()
string = "I am currently developing an iOS app that converts text to speech using AVSynthesizer.What I want to do is that while the synthesizer is speaking, utterance rate can be changed and with a slider and the speed of the speaking changes. I am doing this in the IBAction of the slider: self.utterance = sender.value but the synthesizer doesn't change the speed. Ive been looking for information but I haven't found something yet. What can I do? Thanks in advance."
synthesizer = AVSpeechSynthesizer()
synthesizer.delegate = self
synthesizer.speak(buildUtterance(for: AVSpeechUtteranceDefaultSpeechRate, with: string))
}
}
extension ViewController: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
debugPrint(characterRange.toRange())
currentRange = characterRange
}
}
实现
AVSpeechSynthesizerDelegate
的委托方法willSpeakRangeOfSpeechString
,并将合成器的委托定义为 self:synthesizer.delegate = self
在该委托方法中,保存接下来要说出的字符范围。
将
IBAction func sl(_ sender: UISlider)
绑定到滑块的事件 touchUpInside。在该 IBAction 中,停止说话,并从索引中获取当前正在朗读的文本的子字符串,它将继续。
构建新话语并开始说出它
利润。
swift 3
import UIKit
import AVFoundation
class ViewController: UIViewController{
@IBOutlet weak var sliderVolume: UISlider! //for volume
@IBOutlet weak var sliderRate: UISlider! //for rate
@IBOutlet weak var sliderPitch: UISlider! //for pitch
@IBOutlet weak var txtForSpeak: UITextField!
let speechSynth = AVSpeechSynthesizer()
@IBAction func btnStartToSpeak(_ sender: UIButton) {
let speechUtt = AVSpeechUtterance(string: self.txtForSpeak.text!)
speechUtt.rate = self.sliderRate.value
speechUtt.volume = self.sliderVolume.value
speechUtt.pitchMultiplier = self.sliderPitch.value
self.speechSynth.speak(speechUtt)
}
}