我目前正在开发一款使用AVSynthesizer将文本转换为语音的iOS应用。
我想要做的是,当合成器正在说话时,可以改变话语率并使用滑块和说话速度的变化。
我在滑块的IBAction中这样做: self.utterance = sender.value
但合成器不会改变速度。我一直在寻找信息,但我还没有找到一些东西。 我做了什么?提前谢谢。
答案 0 :(得分:4)
好的,所以在玩了一下我不知道的这个很酷的功能之后,我发现了一种改变话语率的方法。主要问题是话语目前由合成器排队,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
因此,解决方法是停止合成器并用修剪后的字符串为他提供新的话语。
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 doesnt change the speed. Ive been looking for information but I havent found something yet. What cand 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
}
}
实施willSpeakRangeOfSpeechString
的委托方法AVSpeechSynthesizerDelegate
,并将合成器的委托定义为self:synthesizer.delegate = self
在该委托方法中,保存接下来会说出的characterRange。
将IBAction func sl(_ sender: UISlider)
绑定到滑块的事件touchUpInside。
在那个IBAction中,停止说话,并从索引中获取当前文本的子字符串,它将会继续。
建立新的话语并开始说话
利润。
答案 1 :(得分:-1)
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)
}
}