SpeechRecognitionEngine.AudioPosition 属性


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

 property TimeSpan AudioPosition { TimeSpan get(); };
public TimeSpan AudioPosition { get; }
member this.AudioPosition : TimeSpan
Public ReadOnly Property AudioPosition As TimeSpan


输入设备生成的音频流中的当前位置。The current location in the audio stream being generated by the input device.


在以下示例中,进程内语音识别器使用听写语法来匹配语音输入。In the following example, the in-process speech recognizer uses a dictation grammar to match speech input. 当语音识别器检测到语音时,SpeechDetected 事件的处理程序会将 AudioPositionRecognizerAudioPositionAudioLevel 写入控制台。A handler for the SpeechDetected event writes to the console the AudioPosition, RecognizerAudioPosition, and AudioLevel when the speech recognizer detects speech at its input.

using System;  
using System.Speech.Recognition;  
namespace SampleRecognition  
  class Program  
    private static SpeechRecognitionEngine recognizer;  
    public static void Main(string[] args)  
      // Initialize an in-process speech recognition engine for US English.  
      using (recognizer = new SpeechRecognitionEngine(  
        new System.Globalization.CultureInfo("en-US")))  
        // Create a grammar for finding services in different cities.  
        Choices services = new Choices(new string[] { "restaurants", "hotels", "gas stations" });  
        Choices cities = new Choices(new string[] { "Seattle", "Boston", "Dallas" });  
        GrammarBuilder findServices = new GrammarBuilder("Find");  
        // Create a Grammar object from the GrammarBuilder and load it to the recognizer.  
        Grammar servicesGrammar = new Grammar(findServices);  
        // Add handlers for events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);  
        // Start asynchronous recognition.  
        Console.WriteLine("Starting asynchronous recognition...");  
        // Keep the console window open.  
    // Gather information about detected speech and write it to the console.  
    static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)  
      Console.WriteLine("Speech detected:");  
      Console.WriteLine("  Audio level: " + recognizer.AudioLevel);  
      Console.WriteLine("  Audio position at the event: " + e.AudioPosition);  
      Console.WriteLine("  Current audio position: " + recognizer.AudioPosition);  
      Console.WriteLine("  Current recognizer audio position: " +   
    // Write the text of the recognition result to the console.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
      Console.WriteLine("\nSpeech recognized: " + e.Result.Text);  
      // Add event handler code here.  


AudioPosition 属性引用输入设备在其生成的音频流中的位置。The AudioPosition property references the input device's position in its generated audio stream. 与此相反,RecognizerAudioPosition 属性在其音频输入中引用识别器的位置。By contrast, the RecognizerAudioPosition property references the recognizer's position within its audio input. 这些位置可能不同。These positions can be different. 例如,如果识别器收到了尚未生成识别结果的输入,则 RecognizerAudioPosition 属性的值小于 AudioPosition 属性的值。For example, if the recognizer has received input for which it has not yet generated a recognition result then the value of the RecognizerAudioPosition property is less than the value of the AudioPosition property.