SpeechRecognitionEngine.AudioPosition Свойство

Определение

Получает текущую позицию в аудиопотоке, создаваемом устройством, которое предоставляет входные данные для SpeechRecognitionEngine.

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

Значение свойства

Текущее расположение в потоке звука, созданном устройством ввода.

Примеры

В следующем примере внутрипроцессный распознаватель речи использует грамматику диктовки для сопоставления входных данных речи. Обработчик события SpeechDetected записывает в консоль AudioPosition, и AudioLevel , RecognizerAudioPositionкогда распознаватель речи обнаруживает речь на входных данных.

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")))  
      {  
        recognizer.SetInputToDefaultAudioDevice();  

        // 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");  
        findServices.Append(services);  
        findServices.Append("near");  
        findServices.Append(cities);  

        // Create a Grammar object from the GrammarBuilder and load it to the recognizer.  
        Grammar servicesGrammar = new Grammar(findServices);  
        recognizer.LoadGrammarAsync(servicesGrammar);  

        // Add handlers for events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);  

        // Start asynchronous recognition.  
        recognizer.RecognizeAsync();  
        Console.WriteLine("Starting asynchronous recognition...");  

        // 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 at the event: " + e.AudioPosition);  
      Console.WriteLine("  Current audio position: " + recognizer.AudioPosition);  
      Console.WriteLine("  Current 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("\nSpeech recognized: " + e.Result.Text);  

      // Add event handler code here.  
    }  
  }  
}  

Комментарии

Свойство AudioPosition ссылается на позицию устройства ввода в созданном звуковом потоке. В отличие от этого, RecognizerAudioPosition свойство ссылается на позицию распознавателя внутри его аудиовхода. Эти позиции могут быть разными. Например, если распознаватель получил входные данные, для которых он еще не создал результат распознавания, то значение RecognizerAudioPosition свойства меньше значения AudioPosition свойства.

Применяется к

См. также раздел