Код: Выделить всё
public partial class Form1 : Form
{
private SpeechRecognitionEngine voiceInput;
public Form1()
{
InitializeComponent();
voiceInput = new SpeechRecognitionEngine();
//load the built-in dictation grammar for free-form speech
voiceInput.LoadGrammar(new DictationGrammar());
//default microphone as input
voiceInput.SetInputToDefaultAudioDevice();
voiceInput.SpeechRecognized += Voice_Recognized;
voiceInput.SpeechHypothesized += Voice_Hypothesized;
}
//when speech recognized
private void Voice_Recognized(object sender, SpeechRecognizedEventArgs e)
{
//append text to the textbox
textBox1.AppendText(e.Result.Text + Environment.NewLine);
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
private void Voice_Hypothesized(object sender, SpeechHypothesizedEventArgs e)
{
//show temporary text while speaking
textBox1.Text = e.Result.Text;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
//start button
private void button1_Click(object sender, EventArgs e)
{
try
{
voiceInput.RecognizeAsync(RecognizeMode.Multiple);
button1.Enabled = false; //disabled while listening
}
catch (InvalidOperationException)
{
MessageBox.Show("Speech recognition is already running.");
}
}
//stop button
private void button2_Click(object sender, EventArgs e)
{
try
{
voiceInput.RecognizeAsyncStop();
button1.Enabled = true;
}
catch (InvalidOperationException)
{
MessageBox.Show("Speech recognition is not currently running.");
}
}
//clear form
protected override void OnFormClosing(FormClosingEventArgs e)
{
voiceInput.RecognizeAsyncCancel();
voiceInput.Dispose();
base.OnFormClosing(e);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... -code-in-c
Мобильная версия