斯威夫特 - 在发言时说话量变化

时间:2017-06-20 02:11:36

标签: swift avspeechsynthesizer avspeechutterance

我一直试图让我的推子调整tts的音量" live"但是我不能。我只能在文本首次启动时设置音量。无论如何要做到这一点?

更新:我可以使用以下对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
...

音量不会增加......它不是话语的完整实现。

1 个答案:

答案 0 :(得分: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