The AVSpeechSynthesizer class produces synthesized speech from text on an iOS device, and provides methods for controlling or monitoring the progress of ongoing speech.

To speak some amount of text, you must first create an AVSpeechUtterance instance containing the text. (Optionally, you may also use the utterance object to control parameters affecting its speech, such as voice, pitch, and rate.) Then, pass it to the speakUtterance: method on a speech synthesizer instance to speak that utterance.Apple Documentation

Steps:

  1. Create a new Xcode Project and give it a name. Lets say TextToSpeech.
  2. Now add one framework AVFoundation Framework.

Screen Shot 2016-07-12 at 10.57.13 PM.png3.  Now go to StoryBoard and take a textField and a Button and make proper connection.

Screen Shot 2016-07-12 at 11.02.03 PM.png

4. After making the connection ViewController.h and ViewController.m will look like this. Also import Foundation framework and make a object of AVSpeechSynthesizer.

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController  <AVSpeechSynthesizerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *playPauseButton;

@property (weak, nonatomic) IBOutlet UITextField *textToSpeak;

@property (strong, nonatomic) AVSpeechSynthesizer *avSynthesizer;

@end

5. Now go to Your ViewController.m and write the following Code.

#import “ViewController.h”

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@end

@implementation ViewController

    BOOL speechPausedinMiddle = false;

– (void)viewDidLoad {

    [super viewDidLoad];

    self.avSynthesizer = [[AVSpeechSynthesizer alloc] init];

    speechPausedinMiddle = NO;

    self.avSynthesizer.delegate = self;

    // 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)playPauseButtonPressed:(UIButton *)sender {

    [self.textToSpeak resignFirstResponder];

    if (speechPausedinMiddle == NO) {

        [self.playPauseButton setTitle:@”Pause” forState:UIControlStateNormal];

        [self.avSynthesizer continueSpeaking];

        speechPausedinMiddle = YES;

    } else {

        [self.playPauseButton setTitle:@”Play” forState:UIControlStateNormal];

        speechPausedinMiddle = NO;

        [self.avSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

    }

    if (self.avSynthesizer.speaking == NO) {

        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textToSpeak.text];

        //utterance.rate = AVSpeechUtteranceMinimumSpeechRate;

        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@”en-au”];

        [self.avSynthesizer speakUtterance:utterance];

    }

}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {

    [self.playPauseButton setTitle:@”Play” forState:UIControlStateNormal];

    speechPausedinMiddle = NO;

    NSLog(@”Whole music has been played”);

}

To download the whole Xcode Project Click Here.

Run your Code and enjoy coding 🙂

Advertisements