C# 调用微软自带的语音识别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;//引用系统的Speech的识别
using System.Speech.Synthesis; //引用语音合成

namespace ConsoleApplication1
{
    class Program
    {
        static SpeechSynthesizer sy = new SpeechSynthesizer();
        static void Main(string[] args)
        {
            //创建中午识别器
            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN")))
            {
                //----------------
                //初始化命令词
                Choices conmmonds = new Choices();
                //添加命令词
                conmmonds.Add(new string[] { "傻逼你好", "傻逼你是谁", "傻逼很高兴见到你", "傻逼再见" });
                //初始化命令词管理
                GrammarBuilder gBuilder = new GrammarBuilder();
                //将命令词添加到管理中
                gBuilder.Append(conmmonds);
                //实例化命令词管理
                Grammar grammar = new Grammar(gBuilder);
                //-----------------

                //创建并加载听写语法(添加命令词汇识别的比较精准)
                recognizer.LoadGrammar(grammar);
                //为语音识别事件添加处理程序。
                recognizer.SpeechRecognized += new EventHandler(recognizer_SpeechRRecongized);
                //将输入配置到语音识别器。
                recognizer.SetInputToDefaultAudioDevice();
                //启动异步,连续语音识别。
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                //保持控制台窗口打开。
                while (true) 
                {
                    Console.ReadLine();
                }

            }
        }


        //speechrecognized事件处理
        static void recognizer_SpeechRRecongized(object sender,SpeechRecognizedEventArgs e) 
        {
            Console.WriteLine("识别结果:"+e.Result.Text);
            sy.Speak(e.Result.Text);
        }

    }
}

你可能感兴趣的:(C#)