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!





Leave a Reply