Windows Phone 8 Text To Speech (TTS)

In this article, we will see how to integrate the Text-To-Speech engine with Windows Phone 8.

In Windows Phone 8, it is easy to integrate Text-To-Speech to get the voice for a given input string.

Create a new Windows Phone 8 project and enable the speech recognition capabilities “ID_CAP_SPEECH_RECOGNITION” in the WMAppManifest.xml file as shown below.



Figure 1: Create one Button and TextBlock to do the Text-To-Speech operation.

XAML Code

  1. <phone:PhoneApplicationPage  
  2. x:Class="TextToSpeech.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9. mc:Ignorable="d"  
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11. FontSize="{StaticResource PhoneFontSizeNormal}"  
  12. Foreground="{StaticResource PhoneForegroundBrush}"  
  13. SupportedOrientations="Portrait" Orientation="Portrait"  
  14. shell:SystemTray.IsVisible="True">  
  15.     <!--LayoutRoot is the root grid where all page content is placed-->  
  16.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  17.         <Grid.RowDefinitions>  
  18.             <RowDefinition Height="Auto"/>  
  19.             <RowDefinition Height="*"/>  
  20.         </Grid.RowDefinitions>  
  21.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  22.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  23.             <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  24.         </StackPanel>  
  25.         <!--ContentPanel - place additional content here-->  
  26.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  27.             <TextBox x:Name="inputStringBox" HorizontalAlignment="Left" Height="72" Margin="30,78,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="392"/>  
  28.             <Button x:Name="getOutputBtn" Content="Speech" HorizontalAlignment="Left" Margin="142,176,0,0" VerticalAlignment="Top" Click="getOutputBtn_Click"/>  
  29.         </Grid>  
  30.     </Grid>  
  31. </phone:PhoneApplicationPage>  

Write the following code in the Speech button's click event.

C# Code

Add the namespace:
  1. private async void getOutputBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var speech = new SpeechSynthesizer();  
  4.     await speech.SpeakTextAsync(inputStringBox.Text.ToString());  
  5.   
  6. }  
The SpeakTextAsync() method is asynchronous. So we need to use the await keyword to do the operation.

Now build and run your application. Enter the text into the input box as you wish. Here I write “Welcome to C# corner”. Click the Speech button and listen to the voice.



Figure 2: Windows Phone 8

It works depending on your default settings that you set in your phone. You can change the voice format as male or female and the language is as shown in the following screen.

Go to Settings > Speech. Select the voice format.



Figure 3: Voice speech in Windows Phone

Next Recommended Readings