網站首頁 編程語言 正文
在.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
相關推薦
- 2022-07-30 react中的事件處理
- 2022-08-04 Android解決所有雙擊優化的問題_Android
- 2022-04-01 使用lsof命令恢復已刪除文件(正在使用的文件)
- 2022-05-02 python中的Numpy二維數組遍歷與二維數組切片后遍歷效率比較_python
- 2022-05-15 實例詳解Python的進程,線程和協程_python
- 2022-06-01 AndriodStudio利用ListView和數據庫實現簡單學生管理_Android
- 2022-04-02 Python字符串與正則表達式詳細介紹_python
- 2022-04-16 詳解C語言通過遞歸與非遞歸實現蛇形矩陣_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支