Telerik blogs
XamarinT2 Dark_1200x303
Let’s learn how to work with the built-in text-to-speech engines in Xamarin Forms.

Every one of us has different learning styles. Some of us learn better by reading, but others will have better comprehension by listening. So, what would happen if we could integrate both in our app? That would be great, no? Our app will have a wide reach.

We can even use this feature for people with a visual disability—the experience in the application will better meet their needs, therefore making it more pleasant and usable for them. It’s important to make sure our digital societies allow integration for all people.

So, to put it in practice, in this article I want to talk about an amazing API that Xamarin Essentials brings us which is Text-To-Speech. I personally love this, because it is a great and helpful feature for people with visual impairments! 😍 Of course, aside from the approach I’m giving in this article, you can integrate it in whatever utility that you need!

Let’s Start!

First of All… What Do I Need to Know? 🤔

What is Text-To-Speech?

The TextToSpeech class allows the built-in text-to-speech engines to speak back text and query available languages that the engine supports.

Some limitations

➖ Utterance queue is not guaranteed if called across multiple threads.

➖ Background audio playback is not officially supported.

How to Use It

You just have to call TextToSpeech next to the SpeakAsync method, which received the following parameters:

Text: This is the value that you want to be expressed. (Mandatory)

public async Task SpeakTest()
{
    await TextToSpeech.SpeakAsync("Hello everybody!"); 
    // This method will block until utterance finishes.
}

If you want to add a logic that will run after utterance finishes, you can add the following code:

TextToSpeech.SpeakAsync("Hello everybody!").ContinueWith((t) =>
{
    // Logic that will run after utterance finishes.

}, TaskScheduler.FromCurrentSynchronizationContext());

SpeechOptions: Here you can set up the options for speech. This allows us to have more control over how it’s spoken back. We can set the following parameters:

 Name = Locate, Minimum = 0 , Maximum = 0.1 | Name = Pitch, Minimum = 0 , Maximum = 2.0 | Name = Volume ( Check the explanation below _

Important: As each platform supports different locales and has different codes and ways of specifying the locale, Xamarin.Essentials provides a cross-platform Locale class and a way to query them with GetLocalesAsync. To show in this post, I declared locale variable, which needs to be added in the complete implementation shown below.

  var locales = await TextToSpeech.GetLocalesAsync(); 
  var locale = locales.FirstOrDefault();

Complete implementation:

public async Task SpeakTest()
{
    SpeechOptions options = new SpeechOptions
    { 
          Volume = .75f,
          Pitch = 1.0f,
          Locate = locate
    };
    await TextToSpeech.SpeakAsync("Hello everybody!", options); 
 }

CancellationToken: Allows you to stop the utterance once it starts.

    CancellationTokenSource cancellationToken;
    public async Task SpeakTest()
    {
        cancellationToken = new CancellationTokenSource();
        await TextToSpeech.SpeakAsync("Hello everybody!", cancelToken: cancellationToken.Token);

        // This method will block until utterance finishes.
    }

    // Cancel speech if a cancellation token exists & hasn't been already requested.
    public void CancelSpeech()
    {
        if (cancellationToken?.IsCancellationRequested ?? true)
            return;

        cancellationToken.Cancel();
    }

Thanks for reading! 💚

References: https://docs.microsoft.com/en-us/xamarin/essentials/text-to-speech


LeomarisReyes
About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts

Comments

Comments are disabled in preview mode.