In this recipe, we will learn to work with AVSpeechSynthesizer
, the class that provides the Text-To-Speech (TTS) functionality for many different languages.
Create a new Single View Application in Xamarin Studio and name it SpeechApp
. Add a UITextField
and a button to the controller.
Perform the following steps:
Add the
MonoTouch.AVFoundation
namespace in theSpeechAppViewController.cs
file, using the following code:using MonoTouch.AVFoundation;
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); };
Compile and run the app on the simulator. Type some...