SpeechRecognizer SpeechRecognizer SpeechRecognizer SpeechRecognizer Class

定义

提供对在 Windows 桌面上可用的共享语言识别服务的访问。Provides access to the shared speech recognition service available on the Windows desktop.

public ref class SpeechRecognizer : IDisposable
public class SpeechRecognizer : IDisposable
type SpeechRecognizer = class
    interface IDisposable
Public Class SpeechRecognizer
Implements IDisposable
继承
SpeechRecognizerSpeechRecognizerSpeechRecognizerSpeechRecognizer
实现

示例

下面的示例是一个控制台应用程序的一部分,该应用程序加载语音识别语法并演示异步仿真输入、关联的识别结果和语音识别器引发的相关事件。The following example is part of a console application that loads a speech recognition grammar and demonstrates asynchronous emulated input, the associated recognition results, and the associated events raised by the speech recognizer. 如果 Windows 语音识别未运行,则启动此应用程序也将启动 Windows 语音识别。If Windows Speech Recognition is not running, then starting this application will also start Windows Speech Recognition. 如果 Windows 语音识别处于睡眠状态,则EmulateRecognizeAsync始终返回 null。If Windows Speech Recognition is in the Sleeping state, then EmulateRecognizeAsync always returns null.

using System;  
using System.Speech.Recognition;  
using System.Threading;  

namespace SharedRecognizer  
{  
  class Program  
  {  

    // Indicate whether the asynchronous emulate recognition  
    // operation has completed.  
    static bool completed;  

    static void Main(string[] args)  
    {  

      // Initialize an instance of the shared recognizer.  
      using (SpeechRecognizer recognizer = new SpeechRecognizer())  
      {  

        // Create and load a sample grammar.  
        Grammar testGrammar =  
          new Grammar(new GrammarBuilder("testing testing"));  
        testGrammar.Name = "Test Grammar";  
        recognizer.LoadGrammar(testGrammar);  

        // Attach event handlers for recognition events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        completed = false;  

        // Start asynchronous emulated recognition.   
        // This matches the grammar and generates a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing testing");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  

        completed = false;  

        // Start asynchronous emulated recognition.  
        // This does not match the grammar or generate a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing one two three");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
      }  

      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null)  
      {  
        Console.WriteLine("Recognition result = {0}",  
          e.Result.Text ?? "<no text>");  
      }  
      else  
      {  
        Console.WriteLine("No recognition result");  
      }  
    }  

    // Handle the SpeechRecognizeCompleted event.  
    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      if (e.Result == null)  
      {  
        Console.WriteLine("No result generated.");  
      }  

      // Indicate the asynchronous operation is complete.  
      completed = true;  
    }  
  }  
}  

注解

应用程序使用共享识别器来访问 Windows 语音识别。Applications use the shared recognizer to access Windows Speech Recognition. SpeechRecognizer使用对象添加到 Windows 语音用户体验。Use the SpeechRecognizer object to add to the Windows speech user experience.

此类可用于控制语音识别过程的各个方面:This class provides control over various aspects of the speech recognition process:

Windows 语音识别的配置是通过使用控制面板中的 "语音属性" 对话框来管理的。The configuration of Windows Speech Recognition is managed by the use of the Speech Properties dialog in the Control Panel. 此接口用于选择默认的桌面语音识别引擎和语言、音频输入设备以及语音识别的睡眠行为。This interface is used to select the default desktop speech recognition engine and language, the audio input device, and the sleep behavior of speech recognition. 如果在应用程序运行时更改了 Windows 语音识别的配置,(例如,如果禁用了语音识别或更改了输入语言),则更改将影响所有SpeechRecognizer对象。If the configuration of Windows Speech Recognition is changed while the application is running, (for instance, if speech recognition is disabled or the input language is changed), the change affects all SpeechRecognizer objects.

若要创建独立于 Windows 语音识别的进程内语音识别器,请使用SpeechRecognitionEngine类。To create an in-process speech recognizer that is independent of Windows Speech Recognition, use the SpeechRecognitionEngine class.

备注

在释放Dispose对语音识别器的最后一个引用之前,始终调用。Always call Dispose before you release your last reference to the speech recognizer. 否则,在垃圾回收器调用识别器对象的Finalize方法之前,不会释放它所使用的资源。Otherwise, the resources it is using will not be freed until the garbage collector calls the recognizer object's Finalize method.

构造函数

SpeechRecognizer() SpeechRecognizer() SpeechRecognizer() SpeechRecognizer()

初始化 SpeechRecognizer 类的新实例。Initializes a new instance of the SpeechRecognizer class.

属性

AudioFormat AudioFormat AudioFormat AudioFormat

获取语音识别器接收的音频格式。Gets the format of the audio being received by the speech recognizer.

AudioLevel AudioLevel AudioLevel AudioLevel

获取语音识别器接收的音频级别。Gets the level of the audio being received by the speech recognizer.

AudioPosition AudioPosition AudioPosition AudioPosition

获取音频流中的当前位置,此音频流由向语音识别器提供输入的设备产生。Gets the current location in the audio stream being generated by the device that is providing input to the speech recognizer.

AudioState AudioState AudioState AudioState

获取语音识别器接收的状态音频。Gets the state of the audio being received by the speech recognizer.

Enabled Enabled Enabled Enabled

获取或设置一个值,该值指示此 SpeechRecognizer 对象是否准备好处理语音。Gets or sets a value that indicates whether this SpeechRecognizer object is ready to process speech.

Grammars Grammars Grammars Grammars

获取在此 Grammar 实例中加载的 SpeechRecognizer 对象的集合。Gets a collection of the Grammar objects that are loaded in this SpeechRecognizer instance.

MaxAlternates MaxAlternates MaxAlternates MaxAlternates

获取或设置共享识别器为每个识别操作返回的备用识别结果的最大数量。Gets or sets the maximum number of alternate recognition results that the shared recognizer returns for each recognition operation.

PauseRecognizerOnRecognition PauseRecognizerOnRecognition PauseRecognizerOnRecognition PauseRecognizerOnRecognition

获取或设置一个值,此值指示共享识别器是否在应用程序处理 SpeechRecognized 事件时暂停识别操作。Gets or sets a value that indicates whether the shared recognizer pauses recognition operations while an application is handling a SpeechRecognized event.

RecognizerAudioPosition RecognizerAudioPosition RecognizerAudioPosition RecognizerAudioPosition

获取在处理的音频输入中的识别器当前位置。Gets the current location of the recognizer in the audio input that it is processing.

RecognizerInfo RecognizerInfo RecognizerInfo RecognizerInfo

获取共享语音识别器的相关信息。Gets information about the shared speech recognizer.

State State State State

获取 SpeechRecognizer 对象的状态。Gets the state of a SpeechRecognizer object.

方法

Dispose() Dispose() Dispose() Dispose()

处置此 SpeechRecognizer 对象。Disposes the SpeechRecognizer object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

处置 SpeechRecognizer 对象并释放会话期间使用的资源。Disposes the SpeechRecognizer object and releases resources used during the session.

EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions)

使用文本代替同步语音识别的音频来模拟特定字输入到共享语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。Emulates input of specific words to the shared speech recognizer, using text instead of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

EmulateRecognize(String) EmulateRecognize(String) EmulateRecognize(String) EmulateRecognize(String)

使用文本而不是异步语音识别的音频来模拟输入短语到共享语音识别器。Emulates input of a phrase to the shared speech recognizer, using text instead of audio for synchronous speech recognition.

EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions)

使用文本代替异步语音识别的音频来模拟短语输入到共享语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。Emulates input of a phrase to the shared speech recognizer, using text instead of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

使用文本代替异步语音识别的音频来模拟特定字输入到共享语音识别器,并指定识别器如何处理单词和加载的语音识别语法间的 Unicode 比较。Emulates input of specific words to the shared speech recognizer, using text instead of audio for asynchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

EmulateRecognizeAsync(String) EmulateRecognizeAsync(String) EmulateRecognizeAsync(String) EmulateRecognizeAsync(String)

使用文本而不是异步语音识别的音频来模拟输入短语到共享语音识别器。Emulates input of a phrase to the shared speech recognizer, using text instead of audio for asynchronous speech recognition.

EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions)

使用文本代替异步语音识别的音频来模拟短语输入到共享语音识别器,并指定识别器如何处理短语和加载的语音识别语法间的 Unicode 比较。Emulates input of a phrase to the shared speech recognizer, using text instead of audio for asynchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
LoadGrammar(Grammar) LoadGrammar(Grammar) LoadGrammar(Grammar) LoadGrammar(Grammar)

加载语音识别语法。Loads a speech recognition grammar.

LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar)

异步加载语音识别语法。Asynchronously loads a speech recognition grammar.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
RequestRecognizerUpdate() RequestRecognizerUpdate() RequestRecognizerUpdate() RequestRecognizerUpdate()

请求共享识别器暂停和更新其状态。Requests that the shared recognizer pause and update its state.

RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object)

请求共享识别器暂停和更新其状态并为关联的事件提供用户标记。Requests that the shared recognizer pause and update its state and provides a user token for the associated event.

RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan)

请求共享识别器暂停和更新其状态并为关联的事件提供偏移量和用户标记。Requests that the shared recognizer pause and update its state and provides an offset and a user token for the associated event.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
UnloadAllGrammars() UnloadAllGrammars() UnloadAllGrammars() UnloadAllGrammars()

从共享识别器卸载所有语音识别语法。Unloads all speech recognition grammars from the shared recognizer.

UnloadGrammar(Grammar) UnloadGrammar(Grammar) UnloadGrammar(Grammar) UnloadGrammar(Grammar)

从共享识别器卸载指定语音识别语法。Unloads a specified speech recognition grammar from the shared recognizer.

事件

AudioLevelUpdated AudioLevelUpdated AudioLevelUpdated AudioLevelUpdated

当共享识别器报告其音频输入的级别时发生。Occurs when the shared recognizer reports the level of its audio input.

AudioSignalProblemOccurred AudioSignalProblemOccurred AudioSignalProblemOccurred AudioSignalProblemOccurred

当识别器在音频信号中遇到问题时发生。Occurs when the recognizer encounters a problem in the audio signal.

AudioStateChanged AudioStateChanged AudioStateChanged AudioStateChanged

当音频中的状态更改正被识别器接收时发生。Occurs when the state changes in the audio being received by the recognizer.

EmulateRecognizeCompleted EmulateRecognizeCompleted EmulateRecognizeCompleted EmulateRecognizeCompleted

当共享的识别器终结模拟输入的异步识别操作时发生。Occurs when the shared recognizer finalizes an asynchronous recognition operation for emulated input.

LoadGrammarCompleted LoadGrammarCompleted LoadGrammarCompleted LoadGrammarCompleted

当识别器完成语音识别语法的异步加载时发生。Occurs when the recognizer finishes the asynchronous loading of a speech recognition grammar.

RecognizerUpdateReached RecognizerUpdateReached RecognizerUpdateReached RecognizerUpdateReached

当识别器暂停以同步识别和其他操作时发生。Occurs when the recognizer pauses to synchronize recognition and other operations.

SpeechDetected SpeechDetected SpeechDetected SpeechDetected

当识别器删除它可以标识为语音的输入时发生。Occurs when the recognizer detects input that it can identify as speech.

SpeechHypothesized SpeechHypothesized SpeechHypothesized SpeechHypothesized

当识别器已识别可以为语法中多个完整的短语的组件的一个字或多个字时发生。Occurs when the recognizer has recognized a word or words that may be a component of multiple complete phrases in a grammar.

SpeechRecognitionRejected SpeechRecognitionRejected SpeechRecognitionRejected SpeechRecognitionRejected

当识别器接收不与它已加载的任何语音识别语法匹配的输入时发生。Occurs when the recognizer receives input that does not match any of the speech recognition grammars it has loaded.

SpeechRecognized SpeechRecognized SpeechRecognized SpeechRecognized

当识别器接收与其语音识别语法匹配的输入时发生。Occurs when the recognizer receives input that matches one of its speech recognition grammars.

StateChanged StateChanged StateChanged StateChanged

当 Windows“桌面语音技术”识别引擎的运行状态更改时发生。Occurs when the running state of the Windows Desktop Speech Technology recognition engine changes.

适用于

另请参阅