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

有关其他输出配置选项,请参阅 SetOutputToAudioStreamSetOutputToDefaultAudioDeviceSetOutputToNullSetOutputToWaveFile 方法。

适用于