iDev: iOS7 Feature – AVSpeechSynthesizer (Text To Speech )

Hi Friends,

Apple added lots of new useful API’s in iOS 7, I am going to speak about one quickly today, AVSpeechSynthesizer (Text To Speech).

In short, this is Text to Speech, a really simple way to have iOS read a piece of text out loud. There are a number of use cases for this, navigation, directions, visual impairments, introductions etc.

Firstly you will need to include AVFoundation.framework Frameworks in your project

avfoundation

The simplest implementation of Text To Speech is only a few lines of code, in this example you could test this by adding the following code to your ViewDidLoad method, make sure that you have imported the AVFramework into your header file (#import <AVFoundation/AVFoundation.h>).

/****************************** .h File ***********************/

//
//  ST_VMViewController.h
//  iDev_TextToSpeech
//
//  Created by Vinay Mishra on 3/12/14.
//  Copyright (c) 2014 Vinay Mishra. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ST_VMViewController : UIViewController

@property (nonatomic, weak) IBOutlet UIButton *sayVoiceButton;
@property (nonatomic, weak) IBOutlet UITextView *txtView;

-(IBAction)sayVoiceButtonPress;
@end

/****************************** .M File ***********************/

//
//  ST_VMViewController.m
//  iDev_TextToSpeech
//
//  Created by Vinay Mishra on 3/12/14.
//  Copyright (c) 2014 Vinay Mishra. All rights reserved.
//

#import “ST_VMViewController.h”

@interface ST_VMViewController ()

@end

@implementation ST_VMViewController

@synthesize txtView = _txtView;
@synthesize sayVoiceButton;

– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)sayVoiceButtonPress{
if(_txtView.text.length>0){
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:_txtView.text];
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
utterance.rate = 0.4;// Speed of Speaking [range (0.1 to 1.0)]
utterance.volume = 10;
NSLog(@”Speech ==> %@”, [AVSpeechSynthesisVoice speechVoices]);
NSLog(@”Current Langague == > %@”, [AVSpeechSynthesisVoice currentLanguageCode]);
[syn speakUtterance:utterance];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Message” message:@”Please enter the text” delegate:nil cancelButtonTitle:@”OK” otherButtonTitles: nil];
[alert show];
}
}
@end

Simply you create a function in which you only pass the string only like this

-(void) playVoiceWithString:(NSString *) textString{

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString: textString];
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
utterance.rate = 0.4;// Speed of Speaking [range (0.1 to 1.0)]
utterance.volume = 10;
[syn speakUtterance:utterance];

}

Thanks 🙂

Keep Coding:)

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s