Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing the text-to-speech feature


In this recipe, we will learn to work with AVSpeechSynthesizer, the class that provides the Text-To-Speech (TTS) functionality for many different languages.

Getting ready

Create a new Single View Application in Xamarin Studio and name it SpeechApp. Add a UITextField and a button to the controller.

How to do it…

Perform the following steps:

  1. Add the MonoTouch.AVFoundation namespace in the SpeechAppViewController.cs file, using the following code:

    using MonoTouch.AVFoundation;
  2. Add the following code in the ViewDidLoad method:

    this.txtEntry.ShouldReturn = (textField) => textField.ResignFirstResponder();
    this.btnSpeak.TouchUpInside += (sender, e) => {
      AVSpeechSynthesizer synth = new AVSpeechSynthesizer();
      AVSpeechUtterance utterance = new AVSpeechUtterance(this.txtEntry.Text);
      utterance.Rate = 0.3f;
      utterance.Voice = AVSpeechSynthesisVoice.FromLanguage("en-US");
      synth.SpeakUtterance(utterance);
    };
  3. Compile and run the app on the simulator. Type some...