C# 离线语音识别Microsoft Speech Platform SDK例子
时间: 2024-02-16 11:10:24 浏览: 83
C#语音识别用法实例
以下是一个简单的 C# 离线语音识别 Microsoft Speech Platform SDK 的例子:
```csharp
using System;
using System.Speech.Recognition;
class Program
{
static void Main(string[] args)
{
// 创建语音识别引擎
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
// 设置识别模式为离线模式
recognizer.SetInputToDefaultAudioDevice();
// 加载语音识别语法
var grammar = new Grammar("MyGrammar.xml");
recognizer.LoadGrammar(grammar);
// 注册语音识别事件处理程序
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// 开始语音识别
recognizer.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}
}
```
上述代码中,`MyGrammar.xml` 是语音识别语法文件,用于定义识别的关键词和短语。`recognizer_SpeechRecognized` 是语音识别事件处理程序,用于处理识别结果。调用 `recognizer.RecognizeAsync(RecognizeMode.Multiple)` 开始语音识别,识别过程将在后台进行,直到调用 `recognizer.RecognizeAsyncStop()` 方法停止识别。
阅读全文