RecognitionResult.GetAudioForWordRange 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在识别结果中获取与特定范围单词关联的音频部分。
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
所表示范围中的最后一个日期。
返回
与字范围关联的音频的部分。
例外
从启动调用的结果生成的识别器 EmulateRecognize
或 SpeechRecognizer 或 SpeechRecognitionEngine 对象的 EmulateRecognizeAsync
方法。
示例
以下示例创建一个语法来接受名称输入,并向其附加事件的处理程序 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 属性。