0

I have been trying to get my fader to adjust the volume of the tts "live" but I cannot. I can only set the volume when the text first starts. Anyway to do this?

UPDATE: I was able to access the delegate using the following changes to my 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 )
    }
}

Invoking it is just:

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

It prints:

0.5
1.0
1.0
1.0
1.0
1.0
...

The volume does not increase...it's NOT a full implementation of utterance.

1 Answers1

0

Changing the properties of a class AVSpeechUtterance after enqueuing to AVSpeechSynthesizer won't have any effect. Please check the documentation of 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
K_Mohit
  • 528
  • 3
  • 17