共用方式為


RecognizedAudio.GetRange(TimeSpan, TimeSpan) 方法

定義

選取並傳回目前所辨識的音訊區段做為二進位資料。

public:
 System::Speech::Recognition::RecognizedAudio ^ GetRange(TimeSpan audioPosition, TimeSpan duration);
public System.Speech.Recognition.RecognizedAudio GetRange (TimeSpan audioPosition, TimeSpan duration);
member this.GetRange : TimeSpan * TimeSpan -> System.Speech.Recognition.RecognizedAudio
Public Function GetRange (audioPosition As TimeSpan, duration As TimeSpan) As RecognizedAudio

參數

audioPosition
TimeSpan

要傳回音訊資料的起點。

duration
TimeSpan

要傳回之區段的長度。

傳回

傳回已辨識的音訊子區段,如 audioPositionduration 所定義。

例外狀況

audioPositionduration定義一段超出目前區段範圍的音訊。

目前所辨識的音訊不含任何資料。

範例

下列範例會建立名稱輸入的 SpeechRecognized 語音辨識文法、新增 事件的處理常式,並將文法載入至進程語音辨識器。 然後,它會將輸入名稱部分的音訊資訊寫入音訊檔案。 音訊檔案會當做 物件的輸入 SpeechSynthesizer 使用,該物件會說出包含錄製音訊的片語。

private static void AddNameGrammar(SpeechRecognitionEngine recognizer)  
{  
  GrammarBuilder builder = new GrammarBuilder();  
  builder.Append("My name is");  
  builder.AppendWildcard();  

  Grammar nameGrammar = new Grammar(builder);  
  nameGrammar.Name = "Name Grammar";  
  nameGrammar.SpeechRecognized +=  
    new EventHandler<SpeechRecognizedEventArgs>(  
      NameSpeechRecognized);  

  recognizer.LoadGrammar(nameGrammar);  
}  

// Handle the SpeechRecognized event of the name grammar.  
private static void NameSpeechRecognized(  
  object sender, SpeechRecognizedEventArgs e)  
{  
  Console.WriteLine("Grammar ({0}) recognized speech: {1}",  
    e.Result.Grammar.Name, e.Result.Text);  

  try  
  {  

    // The name phrase starts after the first three words.  
    if (e.Result.Words.Count < 4)  
    {  

      // Add code to check for an alternate that contains the wildcard.  
      return;  
    }  

    RecognizedAudio audio = e.Result.Audio;  
    TimeSpan start = e.Result.Words[3].AudioPosition;  
    TimeSpan duration = audio.Duration - start;  

    // Add code to verify and persist the audio.  
    string path = @"C:\temp\nameAudio.wav";  
    using (Stream outputStream = new FileStream(path, FileMode.Create))  
    {  
      RecognizedAudio nameAudio = audio.GetRange(start, duration);  
      nameAudio.WriteToWaveStream(outputStream);  
      outputStream.Close();  
    }  

    Thread testThread =  
      new Thread(new ParameterizedThreadStart(TestAudio));  
    testThread.Start(path);  
  }  
  catch (Exception ex)  
  {  
    Console.WriteLine("Exception thrown while processing audio:");  
    Console.WriteLine(ex.ToString());  
  }  
}  

// Use the speech synthesizer to play back the .wav file  
// that was created in the SpeechRecognized event handler.  

private static void TestAudio(object item)  
{  
  string path = item as string;  
  if (path != null && File.Exists(path))  
  {  
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();  
    PromptBuilder builder = new PromptBuilder();  
    builder.AppendText("Hello");  
    builder.AppendAudio(path);  
    synthesizer.Speak(builder);  
  }  
}  

適用於

另請參閱