The iOS 7 operating system was a great advance for Apple mobile devices, and iOS 8 promises to be even more impressive. With the vast number of features added to iOS 7 last fall, it is easy to overlook a few of them. In this blog, I’ll show you how to use the speech synthesizer to convert text to audible speech. The full source code can be found here: DictationTest (Note that this code was part of a larger dictation proof of concept, hence the name)
The speech synthesizer is a part of the AVFoundation framework, so the first thing we need to do is link against that framework in our app, and import AVFoundation/AVFoundation.h. We’ve done this in the ViewController.h file for simplicity:
@interface ViewController : UIViewController
@property (nonatomic, weak) IBOutlet UITextView *textView;
We’ve also added a property for a text view, and an action method for dismissing the text view’s keyboard. In this method, we’ll convert the text to audio, and play it using the speech synthesizer as well.
The implementation of the dismissKeyboard method in ViewController.m is listed here:
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance =
//slow down the speech rate a bit
[utterance setRate:AVSpeechUtteranceDefaultSpeechRate - 0.2];
//control the pitch of the voice:
First, we resign the first responder on the text view. The first responder is the keyboard; resigning it removes it from the UI. After doing this, we instantiate an AVSpeechSynthesizer and an AVSpeechUtterance. The synthesizer will do the actual speeking, and the utterance is the text we want to have spoken, converted to a format suitable for the synthesizer.
All that remains is to speak the text; we do this with the speakUtterance method of the synthesizer. But before we do this, we have an opportunity to set a couple of properties on the utterance object to control how the speech will sound. The rate property controls how fast the text will be spoken. The value is a float ranging from AVSpeechUtteranceMinimumSpeechRate to AVSpeechUtteranceMaximumSpeechRate, with a third constant defined at the default speech rate (AVSpeechUtteranceDefaultSpeechRate). The higher the speech rate, the faster the text will be spoken.
The pitchMultiplier controls the pitch (high or low) of the voice. Again, this is a float, this time in the range 0.5 to 2.0. The default value is 1.0, of course.
The user interface for the app is very simple: just a text view, a label, and a button. Wire up the text view to the UITextView outlet, and the dismissKeyboard method to the “Speak Entered Text” button’s touchUpInside event. I’ve changed the background color of the text view to make it stand out a bit. Have fun!
10 Top Web Development Frameworks In 2017
13 Best Programming Languages to Learn
42 PHP Frameworks to Watch Out in 2017
A Guide to Becoming a Full-Stack Developer in 2017
Top 10 performance tuning tips for relational databases
Awesome Programming Tools and Resources
Best 15 Unix Command Line Tools
7 Resources to Sharpen your C programming skills