C# windows語音識別與朗讀實例

C# windows語音識別與朗讀示例,供大傢參考,具體內容如下

本示例通過windows語音識別功能進行語音識別和文本朗讀。

打開windows麥克風,點擊start按鍵,大聲朗讀 “中國”、“美國”、“英國”,識別成功將發出“嘟”的提示音並朗讀對應結果。

用到的語音識別模塊包括:

using System.Speech.Recognition;
using System.Speech.Synthesis;

動態連接庫文件在我的資源中下載.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(); //語音識別模塊
        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()  //語音識別初始化
        {
            //SS.SelectVoice("lily");
            SRE.SetInputToDefaultAudioDevice();  // 默認的語音輸入設備,也可以設定為去識別一個WAV文

            GrammarBuilder GB = new GrammarBuilder();

            GB.Append(new Choices(new string[] { "中國", "美國", "英國"}));

            DictationGrammar DG = new DictationGrammar();

            Grammar G = new Grammar(GB);

            G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized);  //註冊語音識別事件

            SRE.EndSilenceTimeout = TimeSpan.FromSeconds(2);

            SRE.LoadGrammar(G);

        }

        void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Beep(500, 500);//已識別提示音

            string result = e.Result.Text;            
            switch (result)
            {
                case "中國":
                    shibie = "中國:五星紅旗";
                    choice(0);
                    break;
                case "美國":
                    shibie = "美國:星條旗";
                    choice(1);                    
                    break;
                case "英國":
                    shibie = "英國:米字旗";
                    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();

        }    


    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: