gpt4 book ai didi

Swift - 在讲话时改变音量

转载 作者:行者123 更新时间:2023-11-30 12:22:20 25 4
gpt4 key购买 nike

我一直试图让我的推子调整“实时”tts 的音量,但我做不到。我只能在文本第一次开始时设置音量。无论如何要这样做吗?

更新:我能够使用对 View Controller 的以下更改来访问委托(delegate):

protocol theSpeechSynth {
func theSpeechSynthVar() -> AVSpeechSynthesizer
}

class GameViewController: UIViewController, theSpeechSynth, AVSpeechSynthesizerDelegate {

let theSpeechSynthesizer = AVSpeechSynthesizer()

func theSpeechSynthVar() -> AVSpeechSynthesizer {
return theSpeechSynthesizer
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange,
utterance: AVSpeechUtterance) {

print(utterance.volume)
utterance.volume = 1.0
print(utterance.volume)
}

func viewDidLoad() {
super.viewDidLoad()
tts.speechSynthesizer = theSpeechSynthVar()

...
}
}



import AVFoundation
class announceIt {
let voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
let speechSynthesizer = AVSpeechSynthesizer()
let voiceToUse = AVSpeechSynthesisVoice(language: "en-GB")
var speechUtterance: AVSpeechUtterance = AVSpeechUtterance()

func speak(speakIt: String) {
speechUtterance = AVSpeechUtterance(string: speakIt)
speechUtterance.voice = voiceToUse
// theVolumes.voice is constantly being updated by the fader
speechUtterance.volume = 0.5
speechSynthesizer.speak(speechUtterance)
}

func volumeChange() {
speechUtterance.volume = Float( theVolumes.voice )
}
}

调用它只是:

let tts: AnnounceIt = AnnounceIt()
// I added this for the delegate:
tts.speechSynthesizer = theSpeechSynthesizer // from the ViewController
tts.speak(speakIt: "I want this volume to go up and down when the volume changes but I can't get it do to that, it will only be the volume when it starts.")

它打印:

0.5
1.0
1.0
1.0
1.0
1.0
...

音量没有增加...这不是话语的完整实现。

最佳答案

在加入 AVSpeechSynthesizer 队列后更改类 AVSpeechUtterance 的属性不会产生任何效果。请查看 AVSpeechUtterance 的文档。

 /* 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

关于Swift - 在讲话时改变音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642716/

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