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 的淺層複本 (Shallow Copy)。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.

適用於

另請參閱