As Castorix31 said, you could use SpeechRecognitionEngine. Here is an example, you can try to refer to it.
Add a reference to System.Speech to the project.
MainWindow.xaml:
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Name="tHypothesized" Grid.Column="0" Foreground="Green" >Hypothesized</TextBlock>
<TextBlock Name="tRecognized" Grid.Column="1" Foreground="Green" >Recognized</TextBlock>
<Button Name="btnStart" Grid.Column="3" Content="Start" Click="btnStart_Click" Width="80" IsEnabled="False"></Button>
<Label Name="lStatus" Grid.Row="1" Grid.Column="0" FontSize="10" Foreground="Red">Status:</Label>
<TextBlock Name="stop" Grid.Row="1" Grid.Column="3" FontSize="10">Speak "End Dictate" to stop.</TextBlock>
<TextBox Name="TextBox1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" TextWrapping="Wrap" />
</Grid>
</Grid>
MainWinodw.xmal.cs:
using System;
using System.Linq;
using System.Windows;
using System.Speech.Recognition;
using System.Threading;
using System.Speech.Synthesis;
namespace SpeechToTextDemo
{
public partial class MainWindow : Window
{
private enum State
{
Idle = 0,
Accepting = 1,
Off = 2,
}
private State RecogState = State.Off;
private SpeechRecognitionEngine srecog;
private SpeechSynthesizer synth = null;
private int Hypothesized = 0;
private int Recognized = 0;
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InitializeRecognizerSynthesizer();
if (SelectInputDevice())
{
LoadDictationGrammar();
btnStart.IsEnabled = true;
ReadAloud("Speech Engine Ready for Input");
}
}
private void InitializeRecognizerSynthesizer()
{
var selectedRecognizer = (from o in SpeechRecognitionEngine.InstalledRecognizers()
where o.Culture.Equals(Thread.CurrentThread.CurrentCulture)
select o).FirstOrDefault();
srecog = new SpeechRecognitionEngine(selectedRecognizer);
srecog.AudioStateChanged+=new EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);
srecog.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
srecog.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
synth = new SpeechSynthesizer();
}
private bool SelectInputDevice()
{
bool proceedLoading = true;
if (IsOscompatible())
{
try
{
srecog.SetInputToDefaultAudioDevice();
}
catch
{
proceedLoading = false;
}
}
else
ThreadPool.QueueUserWorkItem(InitSpeechRecogniser);
return proceedLoading;
}
private bool IsOscompatible()
{
OperatingSystem osInfo = Environment.OSVersion;
if (osInfo.Version > new Version("6.0"))
return true;
else
return false;
}
private void InitSpeechRecogniser(object o)
{
srecog.SetInputToDefaultAudioDevice();
}
private void LoadDictationGrammar()
{
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices("End Dictate"));
Grammar commandGrammar = new Grammar(grammarBuilder);
commandGrammar.Name = "main command grammar";
srecog.LoadGrammar(commandGrammar);
DictationGrammar dictationGrammar = new DictationGrammar();
dictationGrammar.Name = "dictation";
srecog.LoadGrammar(dictationGrammar);
}
private void recognizer_AudioStateChanged(object sender, AudioStateChangedEventArgs e)
{
switch (e.AudioState)
{
case AudioState.Speech:
lStatus.Content = "Listening";
break;
case AudioState.Silence:
lStatus.Content = "Idle";
break;
case AudioState.Stopped:
lStatus.Content = "Stopped";
break;
}
}
private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
Hypothesized++;
tHypothesized.Text = "Hypothesized: " + Hypothesized.ToString();
}
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Recognized++;
tRecognized.Text = "Recognized: " + Recognized.ToString();
if (RecogState == State.Off)
return;
float accuracy = (float)e.Result.Confidence;
string phrase = e.Result.Text;
{
if (phrase == "End Dictate")
{
RecogState = State.Off;
srecog.RecognizeAsyncStop();
ReadAloud("Dictation Ended");
return;
}
TextBox1.AppendText(" " + e.Result.Text);
}
}
public void ReadAloud(string speakText)
{
try
{
srecog.RecognizeAsyncCancel();
synth.SpeakAsync(speakText);
}
catch { }
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
switch (RecogState)
{
case State.Off:
RecogState = State.Accepting;
btnStart.Content = "Stop";
srecog.RecognizeAsync(RecognizeMode.Multiple);
break;
case State.Accepting:
RecogState = State.Off;
btnStart.Content = "Start";
srecog.RecognizeAsyncStop();
break;
}
}
}
}
----------------------------------------------------------------------------
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.