实时更改 AVSpeechUtterance 率

abn*_*bey 1 xcode avfoundation ios avspeechsynthesizer avspeechutterance

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

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

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

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

noi*_*gle 6

好的,所以在玩了一些我不知道的很酷的功能之后,我找到了一种改变说话率的方法。主要问题是话语当前被合成器排队,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
Run Code Online (Sandbox Code Playgroud)

因此,解决方法是停止合成器并用修剪过的字符串为他提供新的话语。

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
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 实施委托方法willSpeakRangeOfSpeechStringAVSpeechSynthesizerDelegate,并定义作为合成器自的代表:synthesizer.delegate = self

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

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

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

  5. 建立新的话语并开始说出来

  6. 利润。