Allow iPhone Talk To You

Last time we were talking to the phone using Google’s library, today iPhone will talk to us.

Voice synthesizer isn’t something new and it’s extremely easy to use.

To use the simplest version only 3 lines of code are needed.

First, it’s Utterance which will be spoken. Then the Synthesizer and speaking method.

let utterance = AVSpeechUtterance(string: “Smash Swift”)
let synthesizer = AVSpeechSynthesizer()

NOTE: You won’t hear anything if iPhone is in silent mode.

And that’s it. Any permission isn’t required.

The utterance has few options which can be configured. If you want to change voice or language use AVSpeechSynthesisVoice. For example:

utterance.voice = AVSpeechSynthesisVoice(language: “en-GB”)

Will change to British accent and male voice. Print AVSpeechSynthesisVoice.speechVoices to see other options you have.

Another option is the rate. It’s talk speed and it has values from 0 to 1 where 1 is the fastest.

utterance.rate = 0.4

Another parameter is volume. It’s talk volume and it has values from 0 to 1 where 1 is the loudest.

utterance.volume = 0.7

We can change the speech pitch. It has values from 0 to 2 where 1 is the default and 2 is the highest.

utterance.pitchMultiplier = 1.4