C# windows語音識(shí)別與朗讀實(shí)例
C# windows語音識(shí)別與朗讀示例,供大家參考,具體內(nèi)容如下
本示例通過windows語音識(shí)別功能進(jìn)行語音識(shí)別和文本朗讀。
打開windows麥克風(fēng),點(diǎn)擊start按鍵,大聲朗讀 “中國(guó)”、“美國(guó)”、“英國(guó)”,識(shí)別成功將發(fā)出“嘟”的提示音并朗讀對(duì)應(yīng)結(jié)果。
用到的語音識(shí)別模塊包括:
using System.Speech.Recognition; using System.Speech.Synthesis;
動(dòng)態(tài)連接庫文件在我的資源中下載.System.Speach.dll
示例界面如下:
程序源碼如下:
using System; using System.Runtime.InteropServices; using System.Speech.Recognition; using System.Speech.Synthesis; using System.Threading; using System.Windows.Forms; namespace Test { public partial class FormVoiceControl : Form { static SpeechSynthesizer SS = new SpeechSynthesizer(); private SpeechRecognitionEngine SRE = new SpeechRecognitionEngine(); //語音識(shí)別模塊 private bool SRE_listening = false; private int wordid; private string shibie; [DllImport("kernel32.dll")] public static extern bool Beep(int freq, int duration); public FormVoiceControl() { InitializeComponent(); } public void InitVoice() //語音識(shí)別初始化 { //SS.SelectVoice("lily"); SRE.SetInputToDefaultAudioDevice(); // 默認(rèn)的語音輸入設(shè)備,也可以設(shè)定為去識(shí)別一個(gè)WAV文 GrammarBuilder GB = new GrammarBuilder(); GB.Append(new Choices(new string[] { "中國(guó)", "美國(guó)", "英國(guó)"})); DictationGrammar DG = new DictationGrammar(); Grammar G = new Grammar(GB); G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized); //注冊(cè)語音識(shí)別事件 SRE.EndSilenceTimeout = TimeSpan.FromSeconds(2); SRE.LoadGrammar(G); } void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { Beep(500, 500);//已識(shí)別提示音 string result = e.Result.Text; switch (result) { case "中國(guó)": shibie = "中國(guó):五星紅旗"; choice(0); break; case "美國(guó)": shibie = "美國(guó):星條旗"; choice(1); break; case "英國(guó)": shibie = "英國(guó):米字旗"; choice(2); break; } } private void Button1_Click(object sender, EventArgs e) { if (SRE_listening == false) { button1.Text = "stop"; SRE.RecognizeAsync(RecognizeMode.Multiple); } else { button1.Text = "start"; SRE.RecognizeAsyncStop(); } lblanswer.Text = ""; SRE_listening = !SRE_listening; } private void choice(int id) { wordid = id; Thread t1; Thread t2; t1 = new Thread(new ThreadStart(ShowAnswer)); t1.Start(); t1.Join(); t2 = new Thread(new ThreadStart(SpeekAnswer)); t2.Start(); } void ShowAnswer() //線程 { MethodInvoker mi = new MethodInvoker(this.dosomething); this.BeginInvoke(mi); } void dosomething() { lblanswer.Text = shibie; } void SpeekAnswer() //線程 { switch (wordid) { case 0: SS.Speak("五星紅旗"); break; case 1: SS.Speak("星條旗"); break; case 2: SS.Speak("米字旗"); break; } } private void FormVoiceControl_Load(object sender, EventArgs e) { InitVoice(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 實(shí)現(xiàn)PPT 每一頁轉(zhuǎn)成圖片過程解析
這篇文章主要介紹了C# 實(shí)現(xiàn)PPT 每一頁轉(zhuǎn)成圖片過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09C#仿密??üδ艿暮?jiǎn)單實(shí)現(xiàn)代碼
昨天拿C#寫了個(gè)簡(jiǎn)單的密保卡程序(Console的,偷懶了一下 哈哈),實(shí)現(xiàn)了隨機(jī)生成5x5矩陣卡、轉(zhuǎn)換為字符串、從字符串讀取矩陣卡以及簡(jiǎn)單驗(yàn)證的功能2013-04-04Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(1)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法示例
這篇文章主要介紹了C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法,結(jié)合實(shí)例形式分析了C#生成PDF文檔的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組
這篇文章主要介紹了C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組,文中分別對(duì)Join和GroupJoin的用法進(jìn)行詳細(xì)說明,需要的朋友可以參考下2017-12-12C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-03