SpeechSynthesizer class provides access to the functionality of an installed speech synthesis engine (voice).

Step 1: Open a blank app and add a Button and A TextBox either from the toolbox or by copying the following XAML code into your grid.

  1. <TextBlock Text="Speech Synthesis" FontSize="20" Margin="10,0,0,0"></TextBlock>
  2. <StackPanel Margin="10,40,0,0">
  3. <TextBlock Text="Type the text to speak" VerticalAlignment="Center" FontSize="20"></TextBlock>
  4. <TextBox Name="textToSpeak" TextWrapping="Wrap" Width="300" Height="50" Margin="0,15,0,0" HorizontalAlignment="Left"></TextBox>
  5. <Button Name="speak" Content="Speak" Margin="0,15,0,0" Width="100" Height="30" Click="speak_Click"></Button>
  6. <MediaElement Name="media" AutoPlay="False"></MediaElement>
  7. </StackPanel>


Step 2: Add the following namespaces to your project which is needed in further C# code.

  1. using Windows.Media.SpeechSynthesis;

Step 3: Copy and paste the following code to the cs page which will be called on button click and will speak the text in the TextBox.

  1. private async void speak_Click(object sender, RoutedEventArgs e)
  2. {
  3. SpeechSynthesizer synthesizer = new SpeechSynthesizer();
  4. SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(textToSpeak.Text);
  5. media.AutoPlay = true;
  6. media.SetSource(synthesisStream, synthesisStream.ContentType);
  7. media.Play();
  8. }

Step 4: Run your application and test yourself.