欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# windows语音识别与朗读实例

时间:2023-03-03 11:18:44|栏目:.NET代码|点击:

C# windows语音识别与朗读示例,供大家参考,具体内容如下

本示例通过windows语音识别功能进行语音识别和文本朗读。

打开windows麦克风,点击start按键,大声朗读 “中国”、“美国”、“英国”,识别成功将发出“嘟”的提示音并朗读对应结果。

用到的语音识别模块包括:

using System.Speech.Recognition;
using System.Speech.Synthesis;

动态连接库文件在我的资源中下载.System.Speach.dll

示例界面如下:

程序源码如下:

using System;
using System.Runtime.InteropServices;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
using System.Windows.Forms;

namespace Test
{
    public partial class FormVoiceControl : Form
    {
        static SpeechSynthesizer SS = new SpeechSynthesizer();
        private SpeechRecognitionEngine SRE = new SpeechRecognitionEngine(); //语音识别模块
        private bool SRE_listening = false;
        private int wordid;
        private string shibie;

        [DllImport("kernel32.dll")]
        public static extern bool Beep(int freq, int duration);

        public FormVoiceControl()
        {
            InitializeComponent();
        }

        public void InitVoice()  //语音识别初始化
        {
            //SS.SelectVoice("lily");
            SRE.SetInputToDefaultAudioDevice();  // 默认的语音输入设备,也可以设定为去识别一个WAV文

            GrammarBuilder GB = new GrammarBuilder();

            GB.Append(new Choices(new string[] { "中国", "美国", "英国"}));

            DictationGrammar DG = new DictationGrammar();

            Grammar G = new Grammar(GB);

            G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized);  //注册语音识别事件

            SRE.EndSilenceTimeout = TimeSpan.FromSeconds(2);

            SRE.LoadGrammar(G);

        }

        void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Beep(500, 500);//已识别提示音

            string result = e.Result.Text;            
            switch (result)
            {
                case "中国":
                    shibie = "中国:五星红旗";
                    choice(0);
                    break;
                case "美国":
                    shibie = "美国:星条旗";
                    choice(1);                    
                    break;
                case "英国":
                    shibie = "英国:米字旗";
                    choice(2);
                    
                    break;               
            }

        }
        private void Button1_Click(object sender, EventArgs e)
        {
            if (SRE_listening == false)
            {
                button1.Text = "stop";
                SRE.RecognizeAsync(RecognizeMode.Multiple);
            }
            else
            {
                button1.Text = "start";
                SRE.RecognizeAsyncStop();
            }
            lblanswer.Text = "";
           SRE_listening = !SRE_listening;
        }

        private void choice(int id)
        {
            wordid = id;

            Thread t1;
            Thread t2;          

            t1 = new Thread(new ThreadStart(ShowAnswer));
            t1.Start();
            t1.Join();
            t2 = new Thread(new ThreadStart(SpeekAnswer));
            t2.Start();
        }
        void ShowAnswer()  //线程
        {
            MethodInvoker mi = new MethodInvoker(this.dosomething);
            this.BeginInvoke(mi);

        }
        void dosomething()
        {
            lblanswer.Text = shibie;
        }
        void SpeekAnswer()  //线程
        {
            switch (wordid)
            {
                case 0:
                    SS.Speak("五星红旗");
                    
                    break;
                case 1:
                    SS.Speak("星条旗");
                    
                    break;
                case 2:
                    SS.Speak("米字旗");
                    
                    break;               
            }
        }

        private void FormVoiceControl_Load(object sender, EventArgs e)
        {
            InitVoice();

        }    


    }
}

上一篇:在ASP.NET 2.0中操作数据之三十七:DataList批量更新

栏    目:.NET代码

下一篇:没有了

本文标题:C# windows语音识别与朗读实例

本文地址:http://www.codeinn.net/misctech/226822.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有