基于C#实现语音识别功能详解
作者:CoolDog 时间:2023-07-12 18:55:24
在.NET4.0中,我可以借助System.Speech组件让电脑来识别我们的声音。
以上,当我说"name",显示"Darren",我说"age",显示"永远21"。如何做呢?
首先要开启电脑的语音识别功能。
右键电脑右下方的扬声器,选择"录音设备"。
点击默认的"麦克风",再点击左下角的"配置"按钮。
在VS中创建一个窗体应用程序,界面上有一个RichTextBox和2个Button。
添加System.Speech的引用。
(PS:强调一下如果不打开语音识别功能,启动程序会报错:在此系统上语音识别不可用。找不到 SAPI 和语音识别引擎。)
编写如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsForms
{
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.SpeechHypothesized += RecEngine_SpeechHypothesized;
}
private void RecEngine_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
switch (e.Result.Text)
{
case "name":
txtList.Text += "\nDarren";
break;
case "age":
txtList.Text += "\n永远21";
break;
default:
break;
}
}
private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}
当然中文也是可以识别的,项目很有意思,大家可以调用语音播放进行播放回复内容。
来源:https://www.cnblogs.com/BFMC/p/16145587.html
标签:C#,语音,识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity实现攻击范围检测并绘制检测区域
2023-09-18 21:59:10
![](https://img.aspxhome.com/file/2023/7/67557_0s.jpg)
springboot项目中jackson-序列化-处理 NULL教程
2022-11-03 14:36:21
Unity实现菜品识别的示例代码
2023-06-11 22:16:10
![](https://img.aspxhome.com/file/2023/7/108657_0s.jpg)
理解Android中Activity的方法回调
2023-06-27 13:35:36
Java下载远程服务器文件到本地(基于http协议和ssh2协议)
2022-08-29 12:23:18
![](https://img.aspxhome.com/file/2023/2/97952_0s.png)
Java 自定义注解的魅力
2023-06-29 14:45:15
![](https://img.aspxhome.com/file/2023/6/65776_0s.png)
IDEA最新版2020.1的maven工程本地依赖仓库无法使用问题(已解决)
2023-09-21 17:57:00
![](https://img.aspxhome.com/file/2023/1/60831_0s.png)
C#集合之栈的用法
2022-09-09 17:01:49
![](https://img.aspxhome.com/file/2023/3/108793_0s.jpg)
Android中的Selector的用法详解及实例
2023-11-26 22:17:06
c#只读字段和常量的区别,以及静态构造函数的使用实例
2023-10-31 09:52:20
Java分页工具类及其使用(示例分享)
2021-12-13 10:25:12
Java由浅入深刨析继承
2023-11-23 08:04:24
![](https://img.aspxhome.com/file/2023/4/59564_0s.jpg)
基于Android中的 AutoCompleteTextView实现自动填充
2023-04-02 07:11:36
浅析java修饰符访问权限(动力节点Java学院整理)
2023-07-13 18:07:34
C# List实现行转列的通用方案
2022-03-28 02:53:04
![](https://img.aspxhome.com/file/2023/4/75614_0s.png)
java ThreadPool线程池的使用,线程池工具类用法说明
2023-06-23 18:12:12
Java中的3种输入方式实现解析
2021-10-04 00:16:47
C#基础知识系列八const和readonly关键字详细介绍
2023-06-25 16:39:37
![](https://img.aspxhome.com/file/2023/0/102190_0s.png)
SpringBoot常用数据库开发技术汇总介绍
2023-11-11 09:39:22
浅析Java线程的中断机制
2023-11-19 20:11:50