RecognizedAudio.WriteToWaveStream(Stream) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將音訊以聲波格式寫入資料流中。
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 標頭。