RecognizedAudio.WriteToWaveStream(Stream) 方法

定义

编写音频定向到波形格式的流。

public:
 void WriteToWaveStream(System::IO::Stream ^ outputStream);
public void WriteToWaveStream(System.IO.Stream outputStream);
member this.WriteToWaveStream : System.IO.Stream -> unit
Public Sub WriteToWaveStream (outputStream As Stream)

参数

outputStream
Stream

将接收经过音频数据的流。

示例

以下示例为名称输入创建语音识别语法,为 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);
  }
}

注解

音频数据以 Wave 格式写入 outputStream ,其中包括资源交换文件格式 (RIFF) 标头。

方法 WriteToAudioStream 使用相同的二进制格式,但不包括 Wave 标头。

适用于

另请参阅