SpeechRecognitionEngine.Recognize 方法

定义

启动同步语音识别操作。Starts a synchronous speech recognition operation.

重载

Recognize()

执行同步语音识别操作。Performs a synchronous speech recognition operation.

Recognize(TimeSpan)

使用指定初始静音超时执行同步语音识别操作。Performs a synchronous speech recognition operation with a specified initial silence timeout period.

注解

这些方法执行单个同步标识操作。These methods perform a single, synchronous recognition operation. 识别器对其已加载和已启用的语音识别语法执行此操作。The recognizer performs this operation against its loaded and enabled speech recognition grammars.

在调用此方法时,识别器可以引发以下事件:During a call to this method, the recognizer can raise the following events:

当使用其中一种RecognizeCompleted Recognize方法时,识别器不会引发事件。The recognizer does not raise the RecognizeCompleted event when using one of the Recognize methods.

如果操作不成功RecognitionResult或识别器未启用, Recognize则方法将返回对象。 nullThe Recognize methods return a RecognitionResult object, or null if the operation is not successful or the recognizer is not enabled.

同步识别操作可能会由于以下原因而失败:A synchronous recognition operation can fail for the following reasons:

  • BabbleTimeoutInitialSilenceTimeout属性或 Recognize方法的参数的超时间隔到期之前,不会检测到语音。initialSilenceTimeoutSpeech is not detected before the timeout intervals expire for the BabbleTimeout or InitialSilenceTimeout properties, or for the initialSilenceTimeout parameter of the Recognize method.

  • 识别引擎检测语音,但在任何已加载和启用Grammar的对象中找不到匹配项。The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

若要修改识别器如何处理语音或静默处理识别的时间, BabbleTimeout请使用EndSilenceTimeoutInitialSilenceTimeout、和EndSilenceTimeoutAmbiguous属性。To modify how the recognizer handles the timing of speech or silence with respect to recognition, use the BabbleTimeout, InitialSilenceTimeout, EndSilenceTimeout, and EndSilenceTimeoutAmbiguous properties.

SpeechRecognitionEngine执行识别之前,必须Grammar至少加载一个对象。The SpeechRecognitionEngine must have at least one Grammar object loaded before performing recognition. 若要加载语音识别语法,请使用LoadGrammarLoadGrammarAsync方法。To load a speech recognition grammar, use the LoadGrammar or LoadGrammarAsync method.

若要执行异步识别,请使用其中RecognizeAsync一种方法。To perform asynchronous recognition, use one of the RecognizeAsync methods.

Recognize()

执行同步语音识别操作。Performs a synchronous speech recognition operation.

public:
 System::Speech::Recognition::RecognitionResult ^ Recognize();
public System.Speech.Recognition.RecognitionResult Recognize ();
member this.Recognize : unit -> System.Speech.Recognition.RecognitionResult
Public Function Recognize () As RecognitionResult

返回

输入的标识结果或 null ,如果操作不成功或识别器未启用。The recognition result for the input, or null if the operation is not successful or the recognizer is not enabled.

示例

下面的示例演示了演示基本语音识别的控制台应用程序的一部分。The following example shows part of a console application that demonstrates basic speech recognition. 该示例创建一个DictationGrammar,将其加载到进程内语音识别器,并执行一个识别操作。The example creates a DictationGrammar, loads it into an in-process speech recognizer, and performs one recognition operation.

  
using System;  
using System.Speech.Recognition;  
  
namespace SynchronousRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  
      // Create an in-process speech recognizer for the en-US locale.  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(  
          new System.Globalization.CultureInfo("en-US")))  
      {  
  
        // Create and load a dictation grammar.  
        recognizer.LoadGrammar(new DictationGrammar());  
  
        // Configure input to the speech recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  
  
        // Modify the initial silence time-out value.  
        recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(5);  
  
        // Start synchronous speech recognition.  
        RecognitionResult result = recognizer.Recognize();  
  
        if (result != null)  
        {  
          Console.WriteLine("Recognized text = {0}", result.Text);  
        }  
        else  
        {  
          Console.WriteLine("No recognition result available.");  
        }  
      }  
  
      Console.WriteLine();  
      Console.WriteLine("Press any key to continue...");  
      Console.ReadKey();  
    }  
  }  
}  

注解

此方法执行单个识别操作。This method performs a single recognition operation. 识别器对其已加载和已启用的语音识别语法执行此操作。The recognizer performs this operation against its loaded and enabled speech recognition grammars.

在调用此方法时,识别器可以引发以下事件:During a call to this method, the recognizer can raise the following events:

使用此方法时,识别RecognizeCompleted器不会引发事件。The recognizer does not raise the RecognizeCompleted event when using this method.

如果操作不成功RecognitionResultRecognize()则此方法返回一个对象。 nullThe Recognize() method returns a RecognitionResult object, or null if the operation is not successful.

同步识别操作可能会由于以下原因而失败:A synchronous recognition operation can fail for the following reasons:

  • BabbleTimeoutInitialSilenceTimeout属性的超时间隔到期之前,不会检测到语音。Speech is not detected before the timeout intervals expire for the BabbleTimeout or InitialSilenceTimeout properties.

  • 识别引擎检测语音,但在任何已加载和启用Grammar的对象中找不到匹配项。The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

若要执行异步识别,请使用其中RecognizeAsync一种方法。To perform asynchronous recognition, use one of the RecognizeAsync methods.

另请参阅

Recognize(TimeSpan)

使用指定初始静音超时执行同步语音识别操作。Performs a synchronous speech recognition operation with a specified initial silence timeout period.

public:
 System::Speech::Recognition::RecognitionResult ^ Recognize(TimeSpan initialSilenceTimeout);
public System.Speech.Recognition.RecognitionResult Recognize (TimeSpan initialSilenceTimeout);
member this.Recognize : TimeSpan -> System.Speech.Recognition.RecognitionResult
Public Function Recognize (initialSilenceTimeout As TimeSpan) As RecognitionResult

参数

initialSilenceTimeout
TimeSpan

时间间隔语音识别器接受仅包含在完成标识之前的无声输入。The interval of time a speech recognizer accepts input containing only silence before finalizing recognition.

返回

输入的标识结果或 null ,如果操作不成功或识别器未启用。The recognition result for the input, or null if the operation is not successful or the recognizer is not enabled.

示例

下面的示例演示了演示基本语音识别的控制台应用程序的一部分。The following example shows part of a console application that demonstrates basic speech recognition. 该示例创建一个DictationGrammar,将其加载到进程内语音识别器,并执行一个识别操作。The example creates a DictationGrammar, loads it into an in-process speech recognizer, and performs one recognition operation.

  
using System;  
using System.Speech.Recognition;  
  
namespace SynchronousRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  
      // Create an in-process speech recognizer for the en-US locale.  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(  
          new System.Globalization.CultureInfo("en-US")))  
      {  
        // Create and load a dictation grammar.  
        recognizer.LoadGrammar(new DictationGrammar());  
  
        // Configure input to the speech recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  
  
        // Start synchronous speech recognition.  
        RecognitionResult result = recognizer.Recognize(TimeSpan.FromSeconds(5));  
  
        if (result != null)  
        {  
          Console.WriteLine("Recognized text = {0}", result.Text);  
        }  
        else  
        {  
          Console.WriteLine("No recognition result available.");  
        }  
      }  
  
      Console.WriteLine();  
      Console.WriteLine("Press any key to continue...");  
      Console.ReadKey();  
    }  
  }  
}  

注解

如果语音识别引擎在initialSilenceTimeout参数所指定的时间间隔内检测到语音, Recognize(TimeSpan)则执行单个识别操作,然后终止。If the speech recognition engine detects speech within the time interval specified by initialSilenceTimeout argument, Recognize(TimeSpan) performs a single recognition operation and then terminates. 参数取代了识别器的InitialSilenceTimeout属性。 initialSilenceTimeoutThe initialSilenceTimeout parameter supersedes the recognizer's InitialSilenceTimeout property.

在调用此方法时,识别器可以引发以下事件:During a call to this method, the recognizer can raise the following events:

使用此方法时,识别RecognizeCompleted器不会引发事件。The recognizer does not raise the RecognizeCompleted event when using this method.

如果操作不成功RecognitionResultRecognize()则此方法返回一个对象。 nullThe Recognize() method returns a RecognitionResult object, or null if the operation is not successful.

同步识别操作可能会由于以下原因而失败:A synchronous recognition operation can fail for the following reasons:

  • BabbleTimeoutinitialSilenceTimeout参数的超时间隔到期之前,不会检测到语音。Speech is not detected before the timeout intervals expire for the BabbleTimeout or for the initialSilenceTimeout parameter.

  • 识别引擎检测语音,但在任何已加载和启用Grammar的对象中找不到匹配项。The recognition engine detects speech but finds no matches in any of its loaded and enabled Grammar objects.

若要执行异步识别,请使用其中RecognizeAsync一种方法。To perform asynchronous recognition, use one of the RecognizeAsync methods.

另请参阅

适用于