首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实时改变AVSpeechUtterance速率

实时改变AVSpeechUtterance速率
EN

Stack Overflow用户
提问于 2017-06-12 19:23:58
回答 2查看 2.1K关注 0票数 1

我目前正在开发一个iOS应用程序,它使用AVSynthesizer将文本转换为语音。

我想要做的是,当合成器说话时,说话率可以改变,用滑块和说话速度可以改变。

我在滑块的IBAction中这样做: self.utterance = sender.value

但是合成器不会改变速度。我一直在找资料,但还没找到什么。我是做什么的?提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-12 20:12:49

好的,在玩了一些我不知道的酷功能之后,我找到了一种改变语速的方法。主要的问题是,目前的话语是由合成人排队,rate不能改变。对应于文件:

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

因此,解决办法是停止合成器,用修剪好的弦给他新的话语。

代码语言:javascript
复制
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
    }
}
  1. 实现AVSpeechSynthesizerDelegate的委托方法AVSpeechSynthesizerDelegate,并将合成器的委托定义为self:synthesizer.delegate = self
  2. 在该委托方法中,保存接下来要讲的characterRange。
  3. IBAction func sl(_ sender: UISlider)绑定到滑块的事件touchUpInside。
  4. 在该IBAction中,停止说话,并从索引中获取当前文本的子字符串,它将继续。
  5. 建立新的话语并开始说出来
  6. 利润。
票数 6
EN

Stack Overflow用户

发布于 2017-11-11 11:17:13

swift 3

代码语言:javascript
复制
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)
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44507250

复制
相关文章

相似问题

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