Bringing Text to Life
Getting text to be spoken is as simple as the following code-behind, assuming that a MediaElement
named mediaElement
is on the current Page
:
async Task Speak() { string text = "Turn left at the next intersection"; using (SpeechSynthesizer synthesizer = new SpeechSynthesizer ()) { SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync(text); // Send the audio to a MediaElement this.mediaElement.SetSource(stream, stream.ContentType); // We could dispose the stream once MediaOpened is raised } }
The produced text uses the default voice and speed, both of which can be changed by the user.
If you have multiple voices installed, you can change ...
Get Universal Windows® Apps with XAML and C# Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.