How to use the speech synthesiser in iOS 7

New to iOS 7′s AVFoundation Framework is the speech synthesiser. It’s what Siri is using when he/she speaks to you. We can use this as well in our apps by passing a string into an AVSpeechUtterance object, which in turn we pass into an AVSpeechSynthesizer instance.

Here’s how:

- (IBAction)sayThis:(id)sender {
    
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:self.textField.text];
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
    
    // all these values are optional
    utterance.rate = 0.2f;
    utterance.pitchMultiplier = 0.8f;
    utterance.voice = voice;
    
    [synthesizer speakUtterance:utterance];
}

To tweak the voice of the synthesiser you can specify the language (defaults to the user locale if not specified), as well as pitchMultiplier and rate values. It’s quite fun to play around with!





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.