共用方式為


RecognitionResult.GetAudioForWordRange 方法

定義

取得辨識結果中與特定文字範圍相關聯的音訊區段。

public:
 System::Speech::Recognition::RecognizedAudio ^ GetAudioForWordRange(System::Speech::Recognition::RecognizedWordUnit ^ firstWord, System::Speech::Recognition::RecognizedWordUnit ^ lastWord);
public System.Speech.Recognition.RecognizedAudio GetAudioForWordRange (System.Speech.Recognition.RecognizedWordUnit firstWord, System.Speech.Recognition.RecognizedWordUnit lastWord);
member this.GetAudioForWordRange : System.Speech.Recognition.RecognizedWordUnit * System.Speech.Recognition.RecognizedWordUnit -> System.Speech.Recognition.RecognizedAudio
Public Function GetAudioForWordRange (firstWord As RecognizedWordUnit, lastWord As RecognizedWordUnit) As RecognizedAudio

參數

firstWord
RecognizedWordUnit

範圍內的第一個字。

lastWord
RecognizedWordUnit

範圍內的最後一字。

傳回

與文字範圍相關聯的音訊區段。

例外狀況

辨識器已透過呼叫 SpeechRecognizerSpeechRecognitionEngine 物件的 EmulateRecognizeEmulateRecognizeAsync 方法產生結果。

範例

下列範例會建立文法來接受名稱輸入,並附加至事件的處理常式 SpeechRecognized 。 文法會針對片語的名稱專案使用萬用字元。 事件處理常式會使用萬用字元中的音訊來建立並播放問候語提示。

private Grammar CreateNameInputGrammar()  
{  
  GrammarBuilder wildcardBuilder = new GrammarBuilder();  
  wildcardBuilder.AppendWildcard();  
  SemanticResultKey nameKey =  
    new SemanticResultKey("Name", wildcardBuilder);  

  GrammarBuilder nameBuilder =  
    new GrammarBuilder("My name is");  
  nameBuilder.Append(nameKey);  

  Grammar nameGrammar = new Grammar(nameBuilder);  
  nameGrammar.Name = "Name input";  

  nameGrammar.SpeechRecognized +=  
    new EventHandler<SpeechRecognizedEventArgs>(  
      NameInputHandler);  

  return nameGrammar;  
}  

// Handle the SpeechRecognized event for the name grammar.  
private void NameInputHandler(object sender, SpeechRecognizedEventArgs e)  
{  
  if (e.Result == null) return;  

  RecognitionResult result = e.Result;  
  SemanticValue semantics = e.Result.Semantics;  

  if (semantics.ContainsKey("Name"))  
  {  
    RecognizedAudio nameAudio =  
      result.GetAudioForWordRange(  
        result.Words[3], result.Words[result.Words.Count - 1]);  

    // Save the audio. Create a directory and file as necessary.  
    FileInfo fi = new FileInfo(@"C:\temp\temp.wav");  
    if (!fi.Directory.Exists)  
    {  
      fi.Directory.Create();  
    }  
    FileStream stream = new FileStream(fi.FullName, FileMode.Create);  
    nameAudio.WriteToWaveStream(stream);  
    stream.Close();  

    // Greet the person using the saved audio.  
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();  
    PromptBuilder builder = new PromptBuilder();  
    builder.AppendText("Hello");  
    builder.AppendAudio(fi.FullName);  
    synthesizer.Speak(builder);  
  }  
}  

備註

若要取得與辨識結果相關聯的完整音訊,請使用 Audio 屬性。

適用於

另請參閱