c# system.speech语音识别

在 .net 4.0 

添加引用system.speech.dll

            using System.Speech.Recognition; 

      //创建语音识别引擎
       SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine();

            //创建一组语音识别的语法约束选择
            Choices choices = new Choices();
            //添加语音识别关键字
            choices.Add(new string[] { "雪糕", "大米", "饭团", "开始" });
            //以编程的方式为语音生成约束
            GrammarBuilder gb = new GrammarBuilder(choices);
            //grammarbuilder封装对象
            Grammar grm = new Grammar(gb);
            //SpeechRecognitionEngine异步方式
            recognitionEngine.LoadGrammarAsync(grm);
            //音频输入
            recognitionEngine.SetInputToDefaultAudioDevice();
            //创建语音接收事件
            recognitionEngine.SpeechRecognized += recognitionEngine_SpeechRecognized;

        //创建接收事件函数
        private void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "雪糕":
                    MessageBox.Show("很好吃");
                    break;
                case "大米":
                    MessageBox.Show("好好吃");
                    break;
                case "饭团":
                    MessageBox.Show("好吃");
                    break;
                case "开始":
                    MessageBox.Show("你好");
                    break;
                default:
                    
                    break;

            }
        }


            //开始语音识别
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

            //停止语音识别
            recognitionEngine.RecognizeAsyncStop();

 

感谢:https://zhuanlan.zhihu.com/p/412904240

posted @ 2023-08-18 11:07  海乐学习  阅读(561)  评论(0编辑  收藏  举报