Play C # speech recognition,
In. NET4.0, I can use the System. Speech component to let the computer recognize our voice.
Above, when I say "name", show "Darren", I say "age", show "Forever 21 ". What should we do?
First, enable the computer's speech recognition function.
Right-click the speaker at the bottom right of the computer and select "Recording Device ".
Click the default "Microphone", and then click "Configure" in the lower left corner.
Click "Start Speech Recognition ".
After a series of simple settings, the screen displays the following:
Create a form application in VS. There is a RichTextBox and two buttons on the interface.
Add a reference to System. Speech.
Write as follows:
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices preCmd = new Choices();
preCmd.Add(new string[] { "name", "age" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append(preCmd);
Grammar gr = new Grammar(gb);
recEngine.LoadGrammarAsync(gr);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "name":
richTextBox1.Text += "\nDarren";
break;
case "age":
RichTextBox1.Text + = "\ n Forever 21 ";
break;
}
}
private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
Of course, Chinese speech recognition is also possible.
Fun Y (^_^) Y