封装了一个帮类有用点个赞

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech;
using System.Diagnostics;

namespace VoiceSpeeakHelper
{
    public  class SpeechHelper
    {
     
        #region var
        /// <summary>
        /// 命令字符串数组
        /// </summary>
        private  string[] ChoicesCmdArray = null;
        /// <summary>
        /// 监听说话对象
        /// </summary>
        private SpeechRecognitionEngine SpeechREG = null;
        /// <summary>
        /// 委托函数
        /// </summary>
        public Action<string> ReciveSpeech = null;
        #endregion

        public SpeechHelper(string[] cmdArray, Action<string> reciveMethod)
            : this(cmdArray)
        {
            ReciveSpeech = reciveMethod;
        }
        private SpeechHelper()
        {
            SpeechREG = new SpeechRecognitionEngine();
        }
        public SpeechHelper(Action<string> reciveMethod)
            : this()
        {
            ReciveSpeech = reciveMethod;
        }
        public SpeechHelper(string[] cmdArray) :this()
        {
            ChoicesCmdArray = cmdArray;
        }

        /// <summary>
        /// 初始化Speech所用参数信息 如果没有给规定范围字符串 则可以任意接收输入
        /// </summary>
        /// <returns></returns>
        public bool LoadGramma()
        {
            
            try
            {
                if (ChoicesCmdArray != null && ChoicesCmdArray.Length > 0)
                {
                    ChoicesCmdArray = ChoicesCmdArray.Where(ret => !string.IsNullOrEmpty(ret)).ToArray();
                    Choices choices = new Choices();
                    choices.Add(ChoicesCmdArray);
                    GrammarBuilder gb = new GrammarBuilder(choices);
                    Grammar gr = new Grammar(gb);
                    SpeechREG.LoadGrammar(gr);
                }
                else
                {
                    SpeechREG.LoadGrammar(new DictationGrammar());
                }
                SpeechREG.SetInputToDefaultAudioDevice();
                SpeechREG.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechREG_SpeechRecognized);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Assert(false, "加载语音模块失败![提示:电脑可能没有开启 语音识别功能 ]" + ex.Message);
                return false;
            }
         
        }
        /// <summary>
        /// 开始语音监听
        /// </summary>
        /// <returns></returns>
        public bool StartSpeech()
        {
            bool stBool = false;
            if (SpeechREG!=null)
            {
                try
                {
                    SpeechREG.RecognizeAsync(RecognizeMode.Multiple);
                    stBool = true;
                }
                catch (Exception)
                {
                    stBool = false;
                }
            }
            return stBool;
        }
        /// <summary>
        /// 停止语音监听
        /// </summary>
        public void StopSpeech()
        {
            if (SpeechREG!=null)
            {
                SpeechREG.RecognizeAsyncStop();
            }
        }
        public void CloseSpeech()
        {
            if (SpeechREG != null)
            {
                SpeechREG.Dispose();
                SpeechREG = null;
            }
        }

        void SpeechREG_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            ReciveSpeech(e.Result.Text);
        }
    }
}

使用的动态库下载:链接: https://pan.baidu.com/s/1PoibKcdpRdFEOOugo8brfw 提取码: acq6

主界面图
完整示例下载链接:https://download.csdn.net/download/weixin_43542114/12614522

更多推荐