SpeechRecognizer.AudioPosition 属性

定义

获取音频流中的当前位置,此音频流由向语音识别器提供输入的设备产生。

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

属性值

它接收输入一个识别器的音频输入流中的当前位置。

示例

在以下示例中,共享语音识别器使用听写语法来匹配语音输入。 当语音识别器在其输入处检测到语音时,事件的处理程序SpeechDetected会将 、 RecognizerAudioPositionAudioLevel 写入控制台AudioPosition

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

适用于

另请参阅