Previously I looked at Speech synthesis in Windows with C# but what about speech synthesis on mobile.
Xamarin.Essentials has this covered with the TextToSpeechTextToSpeech class and is included by default when creating a Xamarin Forms mobile application.
It’s really simple to add text to speech capabilities, we simply write code such as
await TextToSpeech.SpeakAsync("Hello World");
As you can see by the use of the await keyword (and the standard Async suffix), the SpeakAsync method returns a task. It also accepts a cancellation token if you need to cancel mid utterance. There’s also an overload which accepts SpeechOptions which allows us to set the volume, pitch and locale.
The second TextToSpeech method is GetLocalesAsync which allows is to get a list of supported locales from the device that can then be used within the SpeakAsync method’s SpeechOptions.
await TextToSpeech.GetLocalesAsync();
Note: It’s fun listening to the attempts at different accents depending upon the locale.