一個基於c#3.0的開發基於vista下語音辨識的通用類

來源:互聯網
上載者:User

廢話少說,看代碼精解,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Globalization;
using System.Windows.Forms;

namespace yincheng01@163.com
{
    public class yuyin
    {
        public SpeechRecognitionEngine recognizer = null;
        public DictationGrammar dictationGrammar = null;
        public System.Windows.Forms.Control cDisplay;

        public yuyin(string[] fg) //建立關鍵詞語列表
        {

            CultureInfo myCIintl = new CultureInfo("zh-CN", false);

            foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//擷取所有語音引擎
            {
                if (config.Culture.Equals(myCIintl) && config.Id == "MS-2052-80-DESK")//選擇中文
                {
                    recognizer = new SpeechRecognitionEngine(config);
                    break;
                }

            }
            if (recognizer != null)
            {

                InitializeSpeechRecognitionEngine(fg);
                dictationGrammar = new DictationGrammar();
            }
            else
            {
                MessageBox.Show("建立語音辨識失敗");
           
            }

        }

        private void InitializeSpeechRecognitionEngine(string[] fg)
        {
            recognizer.SetInputToDefaultAudioDevice();
            Grammar customGrammar = CreateCustomGrammar(fg);
            recognizer.UnloadAllGrammars();
            recognizer.LoadGrammar(customGrammar);
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
        }
        public void BeginRec(Control tbResult)//關聯視窗控制項
        {

            TurnSpeechRecognitionOn();
            TurnDictationOn();
            cDisplay = tbResult;
        }
        public void over()
        {

            TurnSpeechRecognitionOff();

        }
        public virtual Grammar CreateCustomGrammar(string[] fg)
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            grammarBuilder.Append(new Choices(fg));
            return new Grammar(grammarBuilder);
        }

        private void TurnSpeechRecognitionOn()
        {
            if (recognizer != null)
            {
                recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
            else
            {
                MessageBox.Show("建立語音辨識失敗");

            }

        }

        private void TurnSpeechRecognitionOff()
        {
            if (recognizer != null)
            {
                recognizer.RecognizeAsyncStop();

                TurnDictationOff();
            }
            else
            {
                MessageBox.Show("建立語音辨識失敗");

            }

        }
        private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {

        }

        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string text = e.Result.Text;
            cDisplay.Text = text;

        }
        private void TurnDictationOn()
        {
            if (recognizer != null)
            {
                recognizer.LoadGrammar(dictationGrammar);
            }
            else
            {
                MessageBox.Show("建立語音辨識失敗");

            }

        }

        private void TurnDictationOff()
        {
            if (dictationGrammar != null)
            {
                recognizer.UnloadGrammar(dictationGrammar);
            }
            else
            {
                MessageBox.Show("建立語音辨識失敗");

            }
        }

    }
}

 

類的調用

  string[] fg = { “東方”,“西方”,“南方”,“北方” };
                qqq = new yuyin(fg);

                qqq.BeginRec(textBox10);  顯示識別的資訊的控制項

 

需要源碼的請留下EMAIL

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.