代码如下:

语音播报

添加引用:System.Speech.Synthesis或Microsoft.Speech.Synthesis

public static void Speaking(string saying)
        {
            string say = saying;
            Task task = new Task(() =>
            {
                SpeechSynthesizer speech = new SpeechSynthesizer();
                speech.Volume = 100; //音量
                speech.SetOutputToDefaultAudioDevice();//设定默认音频设备
                CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;//获取当前输入语言的区域性
                InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
                if (neededVoice == null)
                {
                    say = "未知的操作";
                }
                else
                {
                    speech.SelectVoice(neededVoice.VoiceInfo.Name);
                }

                string str = say + "," + say + "," + say;

                speech.SpeakAsync(str);//异步播报
            });
            task.Start();

        }

语音识别

添加引用:System.Speech.Recognition


            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<SpeechRecognizedEventArgs>(Deal_SpeechRecongized);
            recognizer.SetInputToDefaultAudioDevice();//设置默认输入设备
            recognizer.RecognizeAsync(RecognizeMode.Multiple);//开始监控设备输入准备识别

            static void Deal_SpeechRecongized(object sender, SpeechRecognizedEventArgs e) //处理识别结果
            {
                Console.WriteLine("识别结果:" + e.Result.Text);
            }

Windows7或XP系统语音播报会报BUG如图所下:

检索COM类工厂中CLSID为{D9F6EE60-58C9-458B-88E1-2F908FD7F87C}的组件失败,原因是出现以下错误:8007007e找不到指定的模块错误

解决方案:

  1. 在项目引用中选中System.Speech,右键打开属性,将嵌入互操作类型及特定版本选为False,如下图所示

  2. 检查系统存不存在语音识别模块,一部分Windows7系统可能由于安装系统时去除了一部分包括语音识别功能组件,导致sapi.dll文件不存在,所以就会爆出上述错误,没有讲述人语言包。
    1. 打开控制面板,切换为小图标,正常会看到语音识别图标,而功能缺乏的系统会找不到语音识别
    2. 如果没有则需要下载Windows7系统TTS修复,下载链接在文章底部,csdn 1积分下载,无积分上传审核不通过
    3. 需要注意的是即使下载修复软件进行修复后,控制面板中出现了语音识别图标,但运行语音识别软件后,但可能会爆出“无法创建所需的引擎ID”类似错误,这时需要将麦克风插入计算机中,然后重启软件,如果依然存在问题,说明修复未成功,Debug失败

 

 下载地址:https://download.csdn.net/download/qq_37192571/21559695?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163004864216780262534902%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fdownload.%2522%257D&request_id=163004864216780262534902&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~download~first_rank_v2~rank_dl_default-1-21559695.pc_v2_rank_dl_default&utm_term=Microsoft.Speech%2FWin7%E7%B3%BB%E7%BB%9F%E5%BE%AE%E8%BD%AFTTS%E8%AF%AD%E9%9F%B3%E4%BF%AE%E5%A4%8D&spm=1018.2226.3001.4451

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐