Playing with AVSpeechSynthesizer !

So I couldn’t really find a topic that interest me before, I wanted to find a really cool but simple coding aspect that we may or may not need in our project. One of the instructors: S/O to Haaris! suggested AVSpeechSynthesizer.

AVSpeechSynthesizer is a class method that basically takes a text and turns it into speech. Now I believe we can have our own speech/voice if we really wanted to but that’s only for if we have only a few lines of text to say. If we wanted a speech that i dynamic and has to be said multiple times, AVSpeechSynthesizer will help us with that, with the added addition of having a robot voice =3. We can give it any accent we want, from English to japanese, depending on our speech settings on our phone.

AVSpeechSynthesizer falls within the AVFoundation framework which deals with audio within the application, you would first have to import that framework and then import the delegate where the view controller class is

class ViewController: UIViewController, AVSpeechSynthesizerDelegate

now with the default voice that we have in the settings, theres a variety of ways that use that voice with Utterance, such as their rate of how fast the voice will speak, the pitch of the voice and as well as how loud the voice will be. here are examples of how to use the utterance methods

let speechUtterance = AVSpeechUtterance(string: “Which one do you guys like better? Objective C or Swift?”)

// set the voice
speechUtterance.voice = self.speechVoice
// rate is 0.0 to 1.0 (default defined by AVSpeechUtteranceDefaultSpeechRate)
//rate is how fast the voice will go
speechUtterance.rate = 0.25
// multiplier is between >0.0 and 2.0 (default 1.0)
//pitchMultiplier: how high the voice will be
speechUtterance.pitchMultiplier = 1.0
// Volume from 0.0 to 1.0 (default 1.0)
speechUtterance.volume = 1.0
// Delays before and after saying the phrase, sorta like the voice taking a breathe for each word
speechUtterance.preUtteranceDelay = 0.0
speechUtterance.postUtteranceDelay = 0.0
//make it speak
speechSynthesizer.speakUtterance(speechUtterance)