This topic has not yet been rated - Rate this topic

SpeechRecognitionEngine.SpeechRecognized Event

The event raised when the recognition engine detects speech, and has found one or more phrases with sufficient confidence levels.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)
public event EventHandler<SpeechRecognizedEventArgs> SpeechRecognized

The Grammar class also supports a SpeechRecognized event (SpeechRecognized).

Handlers subscribed to the SpeechRecognized event of the instance of Grammar used to recognize a given candidate phrase are always called prior calling the handlers for [E][:System.Speech.Recognition.SpeechRecognitionEngine.SpeechRecognized]. Any tasks specific to a particular grammar should always be handled by SpeechRecognized event handlers.

Handlers of SpeechRecognized events can obtain the best possible candidate phrase, as well as a list of up to MaxAlternates phrases with lower confidence levels through an instance of the RecognitionResult object returned by the Result property of the SpeechRecognizedEventArgs object passed to the handler. ([T:System.Speech.Recognition.]SpeechRecognitionRejectedEventArgs is derived from[T:System.Speech.Recognition.SpeechRecognitionEventArgs)]).

The confidence level cut off for recognition is an arbitrary feature of any particular recognition engine and is not settable by applications.

The example below shows the subscription of an anonymous method as a handler for SpeechRecognized, which in turn calls a display function (DisplayResult) to return to the user interface the best candidate and the list of alternates generated.

A second anonymous method subscribes to the SpeechRecognized event of the colorChooserGrammar instance of Grammar.

If the colorChooserGrammarGrammar is used to recognize the best candidate phrase, its handler for SpeechRecognized will be called prior to the handler of the SpeechRecognized eveny for the SpeechRecognitionEngine object.

_recognizer.SpeechRecognized +=
delegate(object sender, SpeechRecognizedEventArgs eventArgs) {
    if (!_asyncEmulation) {

Utils.DisplayAudioInputFormat(_audioStateLabel, _recognizer);
DisplayResult(eventArgs);
    }
};

colorChooserGrammar.SpeechRecognized +=    delegate(object sender, SpeechRecognizedEventArgs eventArgs) {
// Retrieve the value of the semantic property.
string colorName = (string)eventArgs.Result.Semantics["color"].Value;
BackColor = Color.FromName(colorName);
float Bright = BackColor.GetBrightness();
float Hue = BackColor.GetHue();
float Sat = BackColor.GetSaturation();
//Make sure the text is readable regardless of background.
if (BackColor.GetBrightness() <= .50) {
    ForeColor = Color.White;
} else {
    ForeColor = Color.Black;
}
        };
// Displays the semantics, rules, and alternates of the result.
private void DisplayResult(EventArgs eventArgs) {

    //Make sure We handle different event types we support
     RecognitionResult result=null;
    if (null !=(eventArgs as RecognitionEventArgs)) {
result = (eventArgs as RecognitionEventArgs).Result;
    }else if (null !=(eventArgs as EmulateRecognizeCompletedEventArgs)) {
result = (eventArgs as EmulateRecognizeCompletedEventArgs).Result;
    }

    // In all cases, clear results, semantic XML, semantics tree, and alternates.
    _recognitionSmlBrowser.Navigate("about:blank");
    _semanticsTreeView.Nodes.Clear();
    //            _candidateList.Items.Clear();
    _candidateList.DataSource = null;
    _candidateList.DisplayMember = null;
    _phraseInfoLabel.Text = "";
    _bestCandidateTextBox.Text = "";

    if (result != null) {
//If eventArgs not null repopulate displays
//Obtain result from RecognitionEventArg passed in.


//Set Text color on the basis of the event type passed in.

switch (eventArgs.GetType().ToString()) {

    case "System.Speech.Recognition.SpeechHypothesizedEventArgs":
        _bestCandidateTextBox.ForeColor = Color.Black;
        _bestCandidateTextBox.Text = result.Text;
        _phraseInfoLabel.Text = null;
        break;
    case "System.Speech.Recognition.SpeechRecognitionRejectedEventArgs":
        _bestCandidateTextBox.ForeColor = Color.OrangeRed;
        _bestCandidateTextBox.Text = "Unable to Recognize Input";
        _phraseInfoLabel.Text = String.Format("Rejected Phrase Information\n");
        Utils.DisplayBasicPhraseInfo(_phraseInfoLabel, result);
        Utils.DisplaySemanticsSML(_recognitionSmlBrowser, result);
        Utils.DisplaySemanticsTreeView(_semanticsTreeView, result.Semantics);
        break;
    case "System.Speech.Recognition.SpeechRecognizedEventArgs":
        _bestCandidateTextBox.ForeColor = Color.Green;
        _bestCandidateTextBox.Text = result.Text;
        _phraseInfoLabel.Text = String.Format("Recognized Phrase Information\n");
        Utils.DisplayBasicPhraseInfo(_phraseInfoLabel, result);
        Utils.DisplaySemanticsSML(_recognitionSmlBrowser, result);
        Utils.DisplaySemanticsTreeView(_semanticsTreeView, result.Semantics);
        break;
    case "System.Speech.Recognition.EmulateRecognizeCompletedEventArgs":
        _bestCandidateTextBox.ForeColor = Color.Green;
        _bestCandidateTextBox.Text = result.Text;
        _phraseInfoLabel.Text = String.Format("Recognized Phrase Information\n");
        Utils.DisplayBasicPhraseInfo(_phraseInfoLabel, result);
        Utils.DisplaySemanticsSML(_recognitionSmlBrowser, result);
        Utils.DisplaySemanticsTreeView(_semanticsTreeView, result.Semantics);
        break;
    default:
        _bestCandidateTextBox.ForeColor = Color.Black;
        break;
}

Utils.DisplayDataOnListBox(_candidateList, result.Alternates, "Text");

    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ