一个基于speech API5.1的通用语音识别类

本文介绍了一个使用C#实现的简单语音识别应用程序。该程序通过定义关键词组来触发特定的响应,并展示了如何加载这些关键词、开始及停止识别过程。通过实例代码,读者可以了解到如何创建语音语法规则、设置识别状态以及获取识别结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;

namespace yincheng01@163.com
{
public class SpRecognition
{

public SpeechLib.ISpeechRecoGrammar isrg;
public SpeechLib.SpSharedRecoContextClass ssrContex = null;
public System.Windows.Forms.Control cDisplay;
public SpRecognition(string[] gh)//加载关键词
{
ssrContex = new SpSharedRecoContextClass();
isrg = ssrContex.CreateGrammar(0);
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
ssrContex.Recognition += recHandle;
SpeechLib.ISpeechGrammarRule menuRule = isrg.Rules.Add("123", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
object PropValue = "";

foreach (string ff in gh)
{
menuRule.InitialState.AddWordTransition(null, ff, " ", SpeechGrammarWordType.SGLexical, ff, 1, ref PropValue, 1.0F);

}


isrg.Rules.Commit();


isrg.CmdSetRuleState("123", SpeechRuleState.SGDSActive);


isrg.DictationLoad("", SpeechLoadOption.SLOStatic);
isrg.DictationSetState(SpeechRuleState.SGDSActive);


}
public void BeginRec(Control tbResult)
{



cDisplay = tbResult;


}

public void CloseRec()
{


ssrContex = null;
cDisplay = null;

}
private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
{
cDisplay.Text = result.PhraseInfo.GetText(0, -1, true);
}


}
}
类的调用

SpRecognition ff;

string[] dd={"尹成","华东","玉辉","};
ff = new SpRecognition(dd);
ff.BeginRec(richTextBox1);

ff.CloseRec();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值