共用方式為


SpeechSynthesizer.SetOutputToWaveStream(Stream) 方法

定義

設定SpeechSynthesizer物件,以便將輸出附加至包含波形格式音訊的資料流。

public:
 void SetOutputToWaveStream(System::IO::Stream ^ audioDestination);
public void SetOutputToWaveStream (System.IO.Stream audioDestination);
member this.SetOutputToWaveStream : System.IO.Stream -> unit
Public Sub SetOutputToWaveStream (audioDestination As Stream)

參數

audioDestination
Stream

要附加合成輸出的資料流。

範例

下列範例會將片語輸出至 WAV 資料流程。

using System;  
using System.IO;  
using System.Speech.Synthesis;  

namespace SampleSynthesis  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  

      // Initialize a new instance of the speech synthesizer.  
      using (SpeechSynthesizer synth = new SpeechSynthesizer())  
      using (MemoryStream streamAudio = new MemoryStream())  
      {  

        // Create a SoundPlayer instance to play the output audio file.  
        System.Media.SoundPlayer m_SoundPlayer = new System.Media.SoundPlayer();  

        // Configure the synthesizer to output to an audio stream.  
        synth.SetOutputToWaveStream(streamAudio);  

        // Speak a phrase.  
        synth.Speak("This is sample text-to-speech output.");  
        streamAudio.Position = 0;  
        m_SoundPlayer.Stream = streamAudio;  
        m_SoundPlayer.Play();  

        // Set the synthesizer output to null to release the stream.   
        synth.SetOutputToNull();  

        // Insert code to persist or process the stream contents here.  
      }  

      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  
  }  
}  

備註

若要釋放 SpeechSynthesizer 資料流程的參考,請重新設定合成器的輸出,例如,藉由呼叫 SetOutputToNull

如需其他輸出設定選項,請參閱 SetOutputToAudioStream 、、 SetOutputToDefaultAudioDevice SetOutputToNullSetOutputToWaveFile 方法。

適用於