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

更新:我可以通过对Viewcontroller进行以下更改来访问委托:

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 - Swift-说话时说话时改变音量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44642716/

10-14 14:03