다음을 통해 공유


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 이벤트는 in process 음성 인식기에서 문법을 로드 합니다. 그런 다음 오디오 파일에 대 한 입력의 이름 부분에 대 한 오디오 정보를 씁니다. 오디오 파일을 입력으로 사용 되는 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);
  }
}

설명

오디오 데이터를 쓸 outputStream 웨이브 형식의 리소스 교환 파일 형식 (RIFF) 헤더를 포함 합니다.

WriteToAudioStream 메서드 같은 이진 형식을 사용 하지만 Wave 헤더를 포함 하지 않습니다.

적용 대상

추가 정보