Note
Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.
SpeechRecognitionEngine.RecognizeAsync Method (RecognizeMode)
Performs one or more asynchronous speech recognition operations.
Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in Microsoft.Speech.dll)
Syntax
'Declaration
Public Sub RecognizeAsync ( _
mode As RecognizeMode _
)
'Usage
Dim instance As SpeechRecognitionEngine
Dim mode As RecognizeMode
instance.RecognizeAsync(mode)
public void RecognizeAsync(
RecognizeMode mode
)
Parameters
- mode
Type: Microsoft.Speech.Recognition.RecognizeMode
Indicates whether to perform one or multiple recognition operations.
Remarks
If mode is Multiple, the recognizer continues performing asynchronous recognition operations until theRecognizeAsyncCancel or RecognizeAsyncStop method is called.
During a call to this method, the recognizer can raise the following events:
SpeechDetected. Raised when the recognizer detects input that it can identify as speech.
SpeechHypothesized. Raised when input creates an ambiguous match with one of the active grammars.
SpeechRecognitionRejected or SpeechRecognized. Raised when the recognizer finalizes a recognition operation.
RecognizeCompleted. Raised when a RecognizeAsync() operation finishes.
To retrieve the result of an asynchronous recognition operation, attach an event handler to the recognizer's SpeechRecognized event. The recognizer raises this event whenever it successfully completes a synchronous or asynchronous recognition operation. If recognition was not successful, the Result property on RecognizeCompletedEventArgs object, which you can access in the handler for the RecognizeCompleted event, will be a null reference (Nothing in Visual Basic).
An asynchronous recognition operation can fail for the following reasons:
Speech is not detected before the timeout intervals expire for the BabbleTimeout or InitialSilenceTimeout properties.
The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.
To perform synchronous recognition, use one of the Recognize methods.
Examples
The following example shows part of a console application that demonstrates basic speech recognition. The example creates a speech recognition grammar for choosing cities for a flight. It then constructs a Grammar object from the grammar, loads it into the SpeechRecognitionEngine object, and performs performs multiple asynchronous recognition operations. The asynchronous operations are cancelled after 30 seconds. Event handlers are included to demonstrate the events that the recognizer raises during the operation.
using System;
using System.Globalization;
using Microsoft.Speech.Recognition;
using System.Threading;
namespace AsynchronousRecognition
{
class Program
{
// Indicate whether asynchronous recognition is complete.
static bool completed;
static void Main(string[] args)
{
// Create an in-process speech recognizer.
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new CultureInfo("en-US")))
{
// Create a grammar for choosing cities for a flight.
Choices cities = new Choices(new string[]
{ "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append("I want to fly from");
gb.Append(cities);
gb.Append("to");
gb.Append(cities);
// Construct a Grammar object and load it to the recognizer.
Grammar cityChooser = new Grammar(gb);
cityChooser.Name = ("City Chooser");
recognizer.LoadGrammarAsync(cityChooser);
// Attach event handlers.
recognizer.SpeechDetected +=
new EventHandler<SpeechDetectedEventArgs>(
SpeechDetectedHandler);
recognizer.SpeechHypothesized +=
new EventHandler<SpeechHypothesizedEventArgs>(
SpeechHypothesizedHandler);
recognizer.SpeechRecognitionRejected +=
new EventHandler<SpeechRecognitionRejectedEventArgs>(
SpeechRecognitionRejectedHandler);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(
SpeechRecognizedHandler);
recognizer.RecognizeCompleted +=
new EventHandler<RecognizeCompletedEventArgs>(
RecognizeCompletedHandler);
// Assign input to the recognizer and start asynchronous
// recognition.
recognizer.SetInputToDefaultAudioDevice();
completed = false;
Console.WriteLine("Starting asynchronous recognition...");
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Wait 30 seconds, and then cancel asynchronous recognition.
Thread.Sleep(TimeSpan.FromSeconds(30));
recognizer.RecognizeAsyncCancel();
// Wait for the operation to complete.
while (!completed)
{
Thread.Sleep(333);
}
Console.WriteLine("Done.");
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// Handle the SpeechDetected event.
static void SpeechDetectedHandler(object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine(" In SpeechDetectedHandler:");
Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);
}
// Handle the SpeechHypothesized event.
static void SpeechHypothesizedHandler(
object sender, SpeechHypothesizedEventArgs e)
{
Console.WriteLine(" In SpeechHypothesizedHandler:");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the SpeechRecognitionRejected event.
static void SpeechRecognitionRejectedHandler(
object sender, SpeechRecognitionRejectedEventArgs e)
{
Console.WriteLine(" In SpeechRecognitionRejectedHandler:");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the SpeechRecognized event.
static void SpeechRecognizedHandler(
object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" In SpeechRecognizedHandler.");
string grammarName = "<not available>";
string resultText = "<not available>";
if (e.Result != null)
{
if (e.Result.Grammar != null)
{
grammarName = e.Result.Grammar.Name;
}
resultText = e.Result.Text;
}
Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
grammarName, resultText);
}
// Handle the RecognizeCompleted event.
static void RecognizeCompletedHandler(
object sender, RecognizeCompletedEventArgs e)
{
Console.WriteLine(" In RecognizeCompletedHandler.");
if (e.Error != null)
{
Console.WriteLine(
" - Error occurred during recognition: {0}", e.Error);
return;
}
if (e.InitialSilenceTimeout || e.BabbleTimeout)
{
Console.WriteLine(
" - BabbleTimeout = {0}; InitialSilenceTimeout = {1}",
e.BabbleTimeout, e.InitialSilenceTimeout);
return;
}
if (e.InputStreamEnded)
{
Console.WriteLine(
" - AudioPosition = {0}; InputStreamEnded = {1}",
e.AudioPosition, e.InputStreamEnded);
}
if (e.Result != null)
{
Console.WriteLine(
" - Grammar = {0}; Text = {1}; Confidence = {2}",
e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence);
Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);
}
else
{
Console.WriteLine(" - No result.");
}
completed = true;
}
}
}