SpeechSynthesizer.SetOutputToWaveStream(Stream) Method

Definition

Configures the SpeechSynthesizer object to append output to a stream that contains Waveform format audio.

public void SetOutputToWaveStream (System.IO.Stream audioDestination);

Parameters

audioDestination
Stream

The stream to which to append synthesis output.

Examples

The following example outputs a phrase to a WAV stream.

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();
    }
  }
}

Remarks

To release the SpeechSynthesizer's reference to the stream, reconfigure the synthesizer's output, for example, by calling SetOutputToNull.

For other output configuration options, see the SetOutputToAudioStream, SetOutputToDefaultAudioDevice, SetOutputToNull, and SetOutputToWaveFile methods.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)