SpRecognition.cs
Upload User: cookies0
Upload Date: 2022-07-28
Package Size: 284k
Code Size: 2k
Development Platform:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using SpeechLib;
  5. using System.Windows.Forms;
  6. namespace tryreco
  7. {
  8.     class SpRecognition
  9.     {
  10.         private static SpRecognition _Instance = null;
  11.         private SpeechLib.ISpeechRecoGrammar isrg;
  12.         private SpeechLib.SpSharedRecoContextClass ssrContex = null;
  13.         private System.Windows.Forms.Control cDisplay; //fan:用来显示语音转化后的文本
  14.         public System.Windows.Forms.TextBox textbox; //fan:增加textbox在SpRecognition 类
  15.         public SpRecognition()
  16.         {
  17.             ssrContex = new SpSharedRecoContextClass();
  18.             isrg = ssrContex.CreateGrammar(1);
  19.             SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
  20.             new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
  21.             ssrContex.Recognition += recHandle;
  22.         }
  23.         public void BeginRec(Control tbResult)
  24.         {
  25.             isrg.DictationSetState(SpeechRuleState.SGDSActive);
  26.             cDisplay = tbResult;
  27.             // cDisplay.Text = "dddddddddd"; //测试,可以成功在textbox1那里显示出来
  28.         }
  29.         public static SpRecognition instance()
  30.         {
  31.             if (_Instance == null)
  32.                 _Instance = new SpRecognition();
  33.             return _Instance;
  34.         }
  35.         public void CloseRec()
  36.         {
  37.             isrg.DictationSetState(SpeechRuleState.SGDSInactive);
  38.         }
  39.         private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
  40.         {
  41.             cDisplay.Text += result.PhraseInfo.GetText(0, -1, true);
  42.         }
  43.         public void MessageBegin()//弹出开始提示
  44.         {
  45.             textbox = new TextBox();
  46.             textbox.Text = "Notice :this time ,it Begin recoginse";
  47.             MessageBox.Show(textbox.Text);
  48.         }
  49.         public void MessageEnd() //弹出停止提示
  50.         {
  51.             textbox = new TextBox();
  52.             textbox.Text = "Notice :this time ,it End recoginse";
  53.             MessageBox.Show(textbox.Text);
  54.         }
  55.     }
  56. }