Partager via


Note

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

SpeechSynthesizer Class

Provides access to the functionality of an installed a speech synthesis engine.

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Synthesis.SpeechSynthesizer

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

Syntax

'Declaration
Public NotInheritable Class SpeechSynthesizer _
    Implements IDisposable
'Usage
Dim instance As SpeechSynthesizer
public sealed class SpeechSynthesizer : IDisposable

Remarks

When you create a new SpeechSynthesizer object, it uses the default system voice. To configure the SpeechSynthesizer to use one of the installed speech synthesis (text-to-speech) voices, use the SelectVoice or SelectVoiceByHints method. To get information about which voices are installed, use the GetInstalledVoices method and the VoiceInfo class.

The SpeechSynthesizer can use one or more lexicons to guide its pronunciation of words. To add or remove lexicons, use the AddLexicon and RemoveLexicon methods.

This class also provides control over the following aspects of speech synthesis:

The SpeechSynthesizer raises events when it encounters certain features in prompts: (BookmarkReached and SpeakProgress). It also raises events that report on the start (SpeakStarted) and end (SpeakCompleted) of speak operations and on the change of the speaking voice (VoiceChange).

Note

Always call Dispose() before you release your last reference to the SpeechSynthesizer. Otherwise, the resources it is using will not be freed until the garbage collector calls the SpeechSynthesizer object's Finalize() method.

Examples

The following example demonstrates how to initialize and configure a SpeechSynthesizer instance to generate speech from a string.

using System;
using Microsoft.Speech.Synthesis;

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\Synthesizer.wav");

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

      // Speak a string and play back the output file.
      synth.Speak("This example demonstrates a basic use of SpeechSynthesizer");
      m_SoundPlayer.Play();

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SpeechSynthesizer Members

Microsoft.Speech.Synthesis Namespace