本文介绍了先生,我已经运行了这段代码,但是它运行不正常你可以把它写在richtextbox上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
 
namespace re
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            SpeechRecognitionEngine sp = new SpeechRecognitionEngine();
            InitializeComponent();
        }
 

        private SpeechRecognitionEngine LoadDictationGrammars()
        {
 
            // Create a default dictation grammar.
            DictationGrammar defaultDictationGrammar = new DictationGrammar();
            defaultDictationGrammar.Name = "default dictation";
            defaultDictationGrammar.Enabled = true;
 
            // Create the spelling dictation grammar.
            DictationGrammar spellingDictationGrammar =
              new DictationGrammar("grammar:dictation#spelling");
            spellingDictationGrammar.Name = "spelling dictation";
            spellingDictationGrammar.Enabled = true;
 
            // Create the question dictation grammar.
            DictationGrammar customDictationGrammar =
              new DictationGrammar("grammar:dictation");
            customDictationGrammar.Name = "question dictation";
            customDictationGrammar.Enabled = true;
 
            // Create a SpeechRecognitionEngine object and add the grammars to it.
            SpeechRecognitionEngine recoEngine = new SpeechRecognitionEngine();
            recoEngine.LoadGrammar(defaultDictationGrammar);
            recoEngine.LoadGrammar(spellingDictationGrammar);
            recoEngine.LoadGrammar(customDictationGrammar);
 
            // Add a context to customDictationGrammar.
            customDictationGrammar.SetDictationContext("How do you", null);
 
            recoEngine.BabbleTimeout = TimeSpan.FromSeconds(10.0);
            recoEngine.EndSilenceTimeout = TimeSpan.FromSeconds(10.0);
            recoEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(10.0);
            recoEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(10.0);
 
            recoEngine.SpeechRecognized += RecognizerSpeechRecognized;
            // recoEngine.RecognizeCompleted += RecognizerRecognizeCompleted
            recoEngine.SetInputToDefaultAudioDevice();
            recoEngine.RecognizeAsync(RecognizeMode.Multiple);
 
            return recoEngine;
 
        }
        private void RecognizerSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result != null && e.Result.Text =="grammarDictation")
            {
                richTextBox1.Text = e.Result.Text;
            }
            else
            {
                richTextBox1.Text = "Recognized text not available.";
            }
        }
 
        private void form1_Load(object sender, EventArgs e)
        {
 
        }
 

    }
}

推荐答案

SpeechRecognitionEngine Engine = new SpeechRecognitionEngine();
DictationGrammar Grammar = new DictationGrammar();

private void frmManager_Load(object sender, EventArgs e)
{
    /* Set to the default device. */
    Engine.SetInputToDefaultAudioDevice();

    /* Enable Dictation Grammar. */
    Grammar.Name = "Default Dictation";
    Grammar.Enabled = true;

    /* Add the Grammar. */
    Engine.LoadGrammar(Grammar);

    /* Handle Events. */
    Engine.SpeechRecognized += new EventHandler<speechrecognizedeventargs>(Engine_SpeechRecognized);

    /* Do speak. */
    Engine.RecognizeAsync(RecognizeMode.Multiple);

}

void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    string RecognizedText = e.Result.Text;

    /*richTextBox.Text = RecognizedText ?*/

}


这篇关于先生,我已经运行了这段代码,但是它运行不正常你可以把它写在richtextbox上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:05