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);  
  }  
}  

适用于

另请参阅