【问题标题】:Change AVSpeechUtterance rate in real time实时更改 AVSpeechUtterance 速率
【发布时间】:2020-05-14 20:23:26
【问题描述】:

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

我想要做的是,当合成器在说话时,可以改变发声率,并通过滑块改变说话的速度。

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

但合成器不会改变速度。我一直在寻找信息,但我还没有找到任何东西。 我能做什么?提前致谢。

【问题讨论】:

  • Docs 实际上说:/* 在语音话语已入队后设置这些值将无效。 */ open var rate: Float

标签: ios xcode avfoundation avspeechsynthesizer avspeechutterance


【解决方案1】:

好的,所以在玩了一些我不知道的很酷的功能之后,我找到了一种改变说话速度的方法。主要问题是话语当前被合成器排队,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
    }
}
  1. 实现AVSpeechSynthesizerDelegate的委托方法willSpeakRangeOfSpeechString,并将合成器的委托定义为self:synthesizer.delegate = self

  2. 在该委托方法中,保存接下来要说的 characterRange。

  3. IBAction func sl(_ sender: UISlider) 绑定到滑块的事件 touchUpInside。

  4. 在那个 IBAction 中,停止说话,并从索引中获取当前正在说话的文本的子字符串,它会继续。

  5. 构建新的话语并开始说出它

  6. 利润。

【讨论】:

    【解决方案2】:

    斯威夫特 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-14
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多