Helping Meghan Speak (Avalon & SAPI)
I saw this post https://www.longhornblogs.com/robert/archive/2005/11/11/15246.aspx and just had to help out. Here is something I quickly threw together. It uses SAPI 5.1 & Avalon. To use this, you need to add a reference to the SAPI com component (Install the SAPI SDK if you don't already have it). Download the project and code. Good luck Meghan!!
<
Window x:Class="SpeakForMe.Window1" xmlns="https://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="https://schemas.microsoft.com/winfx/xaml/2005" Title="SpeakForMe" Height="Auto" >
<
Window.Resources>
<XmlDataProvider x:Key="Phrases" XPath="Phrases" Source="Phrases.xml">
</XmlDataProvider>
<
DataTemplate x:Key="PhraseTemplate">
<TextBlock Text="{Binding .InnerText}"/>
</DataTemplate>
</Window.Resources>
<
DockPanel>
<
Grid DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<
TextBox x:Name="WhatToSayTextBox" Background="AliceBlue" FontSize="24" Grid.Column="0" Grid.Row="0" />
<Button x:Name="SayItButton" Grid.Column="1" Grid.Row="0" Click="SayItClicked">Say It!</Button>
</Grid>
<
ListBox x:Name="PhraseList" Background="LightPink" ItemsSource="{Binding Source={StaticResource Phrases}, XPath=Phrase}" ItemTemplate="{StaticResource PhraseTemplate}"/>
</
DockPanel>
</
Window>
using
System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Diagnostics;
using SpeechLib;
using System.Xml;
namespace SpeakForMe
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
WhatToSayTextBox.KeyDown += new System.Windows.Input.KeyEventHandler(WhatToSayTextBox_KeyDown);
PhraseList.SelectionChanged += new SelectionChangedEventHandler(PhraseList_SelectionChanged);
}
void PhraseList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
XmlElement x = e.AddedItems[0] as XmlElement;
WhatToSayTextBox.Text = x.InnerText;
}
}
void WhatToSayTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
SayIt(WhatToSayTextBox.Text);
}
private void SayItClicked(object sender, RoutedEventArgs e)
{
SayIt(WhatToSayTextBox.Text);
}
private void SayIt(string whatToSay)
{
uint num;
Debug.WriteLine("Say: " + whatToSay);
ISpVoice voice = new SpVoiceClass();
voice.Speak(whatToSay, 0 , out num);
}
}
}
Phrases.xml
<?
xml version="1.0" encoding="utf-8" ?>
<Phrases>
<Phrase>Hello World!</Phrase>
<Phrase>Yes</Phrase>
<Phrase>No</Phrase>
</Phrases>