SpeechRecognizer.AudioPosition 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得提供語音辨識器輸入的裝置正在產生的音訊資料流中目前的位置。
public:
property TimeSpan AudioPosition { TimeSpan get(); };
public TimeSpan AudioPosition { get; }
member this.AudioPosition : TimeSpan
Public ReadOnly Property AudioPosition As TimeSpan
屬性值
語音辨識器自其中接收到輸入之音訊輸入資料流中的目前位置。
範例
在下列範例中,共用語音辨識器會使用聽寫文法來比對語音輸入。 當語音辨識器在其輸入偵測到語音時,事件的處理常式 SpeechDetected 會寫入主控台 AudioPosition 、 RecognizerAudioPosition 和 AudioLevel 。
using System;
using System.Speech.Recognition;
namespace SampleRecognition
{
class Program
{
private static SpeechRecognizer recognizer;
public static void Main(string[] args)
{
// Initialize a shared speech recognition engine.
recognizer = new SpeechRecognizer();
// Add handlers for events.
recognizer.LoadGrammarCompleted +=
new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.StateChanged +=
new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);
recognizer.SpeechDetected +=
new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
// Create a dictation grammar.
Grammar dictation = new DictationGrammar();
dictation.Name = "Dictation";
// Load the grammar object to the recognizer.
recognizer.LoadGrammarAsync(dictation);
// Keep the console window open.
Console.ReadLine();
}
// Gather information about detected speech and write it to the console.
static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine();
Console.WriteLine("Speech detected:");
Console.WriteLine(" Audio level: " + recognizer.AudioLevel);
Console.WriteLine(" Audio position: " + recognizer.AudioPosition);
Console.WriteLine(" Recognizer audio position: " + recognizer.RecognizerAudioPosition);
}
// Write the text of the recognition result to the console.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Speech recognized: " + e.Result.Text);
// Add event handler code here.
}
// Write the name of the loaded grammar to the console.
static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
{
Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
}
// Put the shared speech recognizer into "listening" mode.
static void recognizer_StateChanged(object sender, StateChangedEventArgs e)
{
if (e.RecognizerState != RecognizerState.Stopped)
{
recognizer.EmulateRecognizeAsync("Start listening");
}
}
}
}
備註
當桌面語音辨識正在執行時,共用辨識器會收到輸入。
屬性 AudioPosition
會參考輸入裝置在其產生的音訊資料流程中的位置。 相較之下, RecognizerAudioPosition 屬性會參考辨識器在處理音訊輸入時的位置。 這些位置可能不同。 例如,如果辨識器已收到尚未產生辨識結果的 RecognizerAudioPosition 輸入,則屬性的值小於 屬性的值 AudioPosition 。