如果是.NET Framework 4.0的环境,请翻阅一下关于这个命名空间的MSDN文档
System.Speech.Recognition
不过先要搞清楚一些基本概念才能开始动手编程。开始语音识别前要先初始化声音输入设备,设定“语言”(地区代码),设定“语法”(识别规则),等等。
Windows 7 预装了中文语音识别引擎
一下是示例代码:
using System
using System.Speech.Recognition
namespace SpeechRecognitionApp
{
class Program
{
static void Main(string[] args)
{
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar())
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized)
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice()
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple)
// Keep the console window open.
while (true)
{
Console.ReadLine()
}
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text)
}
}
}
如果是.NET4, Windows 7之前的环境,可以安装Microsoft Speech SDK 5.1
private void Form3_KeyPress(object sender, KeyPressEventArgs e){
if (e.KeyChar>=(char)48 &&e.KeyChar<=(char)58)
{
MessageBox.Show("数字:" + e.KeyChar)
}
else
{
MessageBox.Show("字符:" + e.KeyChar)
}
}