0

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

更新:我能够使用对我的视图控制器的以下更改来访问委托:

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
...

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

4

1 回答 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
于 2018-06-12T13:16:39.303 回答