Partilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Emulate Spoken Commands (Microsoft.Speech)

Emulation of spoken commands is useful for debugging a speech recognition application during its development. The EmulateRecognize() and EmulateRecognizeAsync() overloaded methods on the SpeechRecognitionEngine class can be used to programmatically emulate speech.

Example

The following example shows a speech recognition engine loading a simple grammar that can recognize "hello world". It then calls EmulateRecognize(String) to send the string "hello world" to the speech recognition engine, which should return that text as the recognized string within the RecognitionResult object.

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
      GrammarBuilder gb = new GrammarBuilder("Hello world");
      Grammar g = new Grammar(gb);
      sre.LoadGrammar(g);
      RecognitionResult rr = sre.EmulateRecognize("Hello world");
      if (rr != null && rr.Text != null)
      {
        Console.WriteLine("Recognized: " + rr.Text);
      }
      Close();
    }

    SpeechRecognitionEngine sre;
  }
}