SpeechRecognizer 類別

定義

為 Windows 桌面上共用的語音辨識服務提供存取。

public ref class SpeechRecognizer : IDisposable
public class SpeechRecognizer : IDisposable
type SpeechRecognizer = class
    interface IDisposable
Public Class SpeechRecognizer
Implements IDisposable
繼承
SpeechRecognizer
實作

範例

下列範例是主控台應用程式的一部分,它會載入語音辨識文法,並示範非同步模擬輸入、相關聯的辨識結果,以及語音辨識器所引發的相關事件。 如果 Windows 語音辨識未執行,則啟動此應用程式也會啟動 Windows 語音辨識。 如果 Windows 語音辨識處於 睡眠 狀態,則 EmulateRecognizeAsync 一律會傳回 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 語音辨識。 SpeechRecognizer使用 物件將 新增至 Windows 語音使用者體驗。

這個類別可控制語音辨識程式的各種層面:

Windows 語音辨識的設定是由主控台中的 [語音屬性] 對話方塊使用來管理。 此介面可用來選取預設桌面語音辨識引擎和語言、音訊輸入裝置,以及語音辨識的睡眠行為。 如果在應用程式執行時變更 Windows 語音辨識的組態, (例如,如果停用語音辨識或輸入語言) 變更,則變更會影響所有 SpeechRecognizer 物件。

若要建立與 Windows 語音辨識無關的進程語音辨識器,請使用 SpeechRecognitionEngine 類別。

注意

一律先呼叫 Dispose ,再釋放對語音辨識器的最後一個參考。 否則,在垃圾收集行程呼叫辨識器物件的 Finalize 方法之前,將不會釋放它所使用的資源。

建構函式

SpeechRecognizer()

初始化 SpeechRecognizer 類別的新執行個體。

屬性

AudioFormat

取得語音辨識器所接收的音訊格式。

AudioLevel

取得語音辨識器所接收的音訊層級。

AudioPosition

取得提供語音辨識器輸入的裝置正在產生的音訊資料流中目前的位置。

AudioState

取得語音辨識器所接收的音訊狀態。

Enabled

取得或設定值,這個值表示這個 SpeechRecognizer 物件是否準備好可以處理語音。

Grammars

取得在這個 Grammar 執行個體中載入之 SpeechRecognizer 物件的集合。

MaxAlternates

取得或設定共用辨識器為每個辨識作業傳回之替代辨識結果的最大數目。

PauseRecognizerOnRecognition

取得或設定值,這個值表示當應用程式正在處理 SpeechRecognized 事件時,共用辨識器是否會暫停辨識作業。

RecognizerAudioPosition

取得辨識器正在處理的音訊輸入的目前位置。

RecognizerInfo

取得共用語音辨識器的相關資訊。

State

取得 SpeechRecognizer 物件的狀態。

方法

Dispose()

處置 SpeechRecognizer 物件。

Dispose(Boolean)

處置 SpeechRecognizer 物件,並釋放工作階段期間所使用的資源。

EmulateRecognize(RecognizedWordUnit[], CompareOptions)

在共用的語音辨識器上模擬特定單字輸入,針對同步的語音辨識使用文字而不使用音訊,並指定辨識器如何處理單字間的 Unicode 比較以及已載入的語音辨識文法。

EmulateRecognize(String)

在共用的語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字而不使用音訊。

EmulateRecognize(String, CompareOptions)

在共用的語音辨識器上模擬片語輸入,針對同步的語音辨識使用文字而不使用音訊,並指定辨識器如何處理片語間的 Unicode 比較以及已載入的語音辨識文法。

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

在共用的語音辨識器上模擬特定單字輸入,針對非同步的語音辨識使用文字而不使用音訊,並指定辨識器如何處理單字間的 Unicode 比較以及已載入的語音辨識文法。

EmulateRecognizeAsync(String)

在共用的語音辨識器上模擬片語輸入,針對非同步的語音辨識使用文字而不使用音訊。

EmulateRecognizeAsync(String, CompareOptions)

在共用的語音辨識器上模擬片語輸入,針對非同步的語音辨識使用文字而不使用音訊,並指定辨識器如何處理片語間的 Unicode 比較以及已載入的語音辨識文法。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
LoadGrammar(Grammar)

載入語音辨識文法。

LoadGrammarAsync(Grammar)

以非同步方式載入語音辨識文法。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RequestRecognizerUpdate()

要求共用辨識器暫停並更新其狀態。

RequestRecognizerUpdate(Object)

要求共用辨識器暫停並更新其狀態,以及提供相關聯事件的使用者語彙基元。

RequestRecognizerUpdate(Object, TimeSpan)

要求共用辨識器暫停並更新其狀態,以及提供相關聯事件的位移和使用者語彙基元。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
UnloadAllGrammars()

從共用辨識器卸載所有語音辨識文法。

UnloadGrammar(Grammar)

從共用辨識器卸載指定的語音辨識文法。

事件

AudioLevelUpdated

當共用辨識器報告其音訊輸入層級時發生。

AudioSignalProblemOccurred

在辨識器遇到音訊訊號的問題時發生。

AudioStateChanged

在辨識器所接收的音訊變更狀態時發生。

EmulateRecognizeCompleted

共用辨識器針對模擬輸入完成非同步辨識作業時發生。

LoadGrammarCompleted

在辨識器完成語音辨識文法的非同步載入時發生。

RecognizerUpdateReached

在辨識器暫停以同步處理辨識和其他作業時發生。

SpeechDetected

當辨識器偵測到可辨識為語音的輸入時發生。

SpeechHypothesized

在辨識器已辨識的文字可能是文法中多個完整片語的元件時發生。

SpeechRecognitionRejected

當辨識器接收到不符合任何已載入的語音辨識文法的輸入時發生。

SpeechRecognized

當辨識器接收到符合其中一個語音辨識文法的輸入時發生。

StateChanged

當 Windows 桌面語音技術辨識引擎的執行狀態變更時發生。

適用於

另請參閱