Share via


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 이벤트는 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);  
  }  
}  

적용 대상

추가 정보