Speech Synthesis In WPF. A Very Simple Test

speechtest.jpg

Speech synthesis is the process of turning text into audio. In WPF is a very simple thing. The first thing you have to do is add a reference to System.Speech.dll to your project. The code is very simple and it explains by itself, so here you have code…

<Window x:Class=”SpeechTesting.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Speech Test” Height=”100″ Width=”300″>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”5*”/>
<ColumnDefinition Width=”*”/>
</Grid.ColumnDefinitions>
<TextBox x:Name=”tb” Grid.Column=”0″/>
<Button Content=”Speech” Grid.Column=”1″ Click=”Button_Click”/>
</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Speech.Synthesis;

namespace SpeechTesting
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
SpeechSynthesizer ss = new SpeechSynthesizer();

public Window1()
{
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
ss.Speak(tb.Text);
}
}
}

1 Comment

  1. Denis Vuyka said,

    June 3, 2009 at 7:00 pm

    I’m afraid this sample does not demonstrate the way calling SpeachSynthesizer.Speak is much easier than that in the Windows Forms.


Post a Comment