共用方式為


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SpeechSynthesizer.SetOutputToWaveFile Method (String, SpeechAudioFormatInfo)

Configures the SpeechSynthesizer object to append output to a Waveform audio format file in a specified format.

Namespace:  Microsoft.Speech.Synthesis
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Sub SetOutputToWaveFile ( _
    path As String, _
    formatInfo As SpeechAudioFormatInfo _
)
'Usage
Dim instance As SpeechSynthesizer
Dim path As String
Dim formatInfo As SpeechAudioFormatInfo

instance.SetOutputToWaveFile(path, formatInfo)
public void SetOutputToWaveFile(
    string path,
    SpeechAudioFormatInfo formatInfo
)

Parameters

Examples

The following example specifies the format of the output of speech synthesis and sends it to a WAV file. Because the SpeakAsync(PromptBuilder) call is asynchronous, the SoundPlayer instance is created (and the Play method invoked) in the handler for the SpeakCompleted event.

using System;
using Microsoft.Speech.Synthesis;
using Microsoft.Speech.AudioFormat;

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

      // Initialize a new instance of the SpeechSynthesizer.
      SpeechSynthesizer synth = new SpeechSynthesizer();

      // Configure the audio output. 
      synth.SetOutputToWaveFile(@"C:\Test\Sample.wav",
          new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Eight, AudioChannel.Mono));

      // Register for the SpeakCompleted event.
      synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);

      // Create a PromptBuilder object and append a text string.
      PromptBuilder song = new PromptBuilder();
      song.AppendText("This is sample output to a WAVE file");

      // Speak the contents of the prompt asynchronously.
      synth.SpeakAsync(song);

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

    // Handle the SpeakCompleted event.
    static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
    {

      // Create a SoundPlayer instance to play the output audio file.
      System.Media.SoundPlayer m_SoundPlayer =
        new System.Media.SoundPlayer(@"C:\Test\Sample.wav");

      //  Play the output file.
      m_SoundPlayer.Play();
    }
  }
}

See Also

Reference

SpeechSynthesizer Class

SpeechSynthesizer Members

SetOutputToWaveFile Overload

Microsoft.Speech.Synthesis Namespace

SetOutputToAudioStream

SetOutputToDefaultAudioDevice

SetOutputToNull

SetOutputToWaveStream