zoukankan      html  css  js  c++  java
  • 语音识别范例

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Speech.Recognition; 
    using SpeechLib;
     
    //要在ref - .net里把systerm.speech引入 否者不能运行
    namespace 语音识别测试
    {
        public partial class Form1 : Form 
        { 
            private SpeechRecognitionEngine SRE = new SpeechRecognitionEngine(); 
    
            public Form1() 
            { 
                InitializeComponent(); 
            } 
    
            private void Form1_Load(object sender, EventArgs e) 
            { 
                SRE.SetInputToDefaultAudioDevice();       //  <=======默认的语音输入设备,你可以设定为去识别一个WAV文件。 
                GrammarBuilder GB = new GrammarBuilder(); 
                GB.Append("选择"); 
                GB.Append(new Choices(new string[] { "红色", "绿色","白色","盒子" })); 
                Grammar G = new Grammar(GB); 
                G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized); 
                SRE.LoadGrammar(G); 
                SRE.RecognizeAsync(RecognizeMode.Multiple); //<=======异步调用识别引擎,允许多次识别(否则程序只响应你的一句话) 
            } 
    
            void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            { 
                //Text = e.Result.Text;
                //MessageBox.Show(e.Result.Text);
                switch (e.Result.Text) 
                { 
                  
                    case "选择红色":
                    try { this.BackColor = Color.Red; }
                    catch { } 
                    break; 
                    case "选择绿色":
                    try { this.BackColor = Color.Green; }
                    catch { }
                    break;
                    case "选择白色":
                    try { this.BackColor = Color.Blue; }
                    catch { } 
                    break;
                    case "选择盒子":
                    try { this.BackColor = Color.Black; }
                    catch { } 
                    break;
                   
                } 
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SpVoice voice = new SpVoice();//SAPI 5.4
    
                voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //其中3为中文,024为英文
                voice.Speak(richTextBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
            } 
        } 
    
    }
    
  • 相关阅读:
    16款值得一用的iPhone线框图模板 (PSD & Sketch)
    设计神器
    {CF812}
    hiho1080(多标记线段树)
    {容斥原理}
    {dp入门}
    {AC自动机}
    CF807
    Trie树
    杂记
  • 原文地址:https://www.cnblogs.com/softimagewht/p/2434915.html
Copyright © 2011-2022 走看看