gpt4 book ai didi

ios - 实时更改 AVSpeechUtterance 速率

转载 作者:行者123 更新时间:2023-12-01 17:47:36 29 4
gpt4 key购买 nike

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

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

我在 slider 的 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
}
}
  • 实现委托(delegate)方法willSpeakRangeOfSpeechStringAVSpeechSynthesizerDelegate并将合成器的委托(delegate)定义为自我:synthesizer.delegate = self
  • 在该委托(delegate)方法中,保存接下来要说的 characterRange。
  • 绑定(bind)IBAction func sl(_ sender: UISlider)到 slider 的事件 touchUpInside。
  • 在那个 IBAction 中,停止说话,并从索引中获取当前正在说话的文本的子字符串,它会继续。
  • 建立新的话语并开始说话
  • 利润。
  • 关于ios - 实时更改 AVSpeechUtterance 速率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44507250/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com