Zdieľať cez


SpeechSynthesizer.Speak Method

Definition

Generates speech output synchronously from a string, a Prompt object, or a PromptBuilder object.

Overloads

Speak(Prompt)

Synchronously speaks the contents of a Prompt object.

Speak(PromptBuilder)

Synchronously speaks the contents of a PromptBuilder object.

Speak(String)

Synchronously speaks the contents of a string.

Remarks

The Speak methods generate speech synchronously. The methods do not return until the content of the Speak instance has been completely spoken. This is the simplest way to generate speech. However, if your application needs to perform tasks while speaking, for example highlight text, paint animation, monitor controls, or other tasks, use the SpeakAsync methods or the SpeakSsmlAsync method to generate speech asynchronously.

During a call to this method, the SpeechSynthesizer can raise the following events:

  • StateChanged. Raised when the speaking state of the synthesizer changes.

  • SpeakStarted. Raised when the synthesizer begins generating speech.

  • PhonemeReached. Raised each time the synthesizer reaches a letter or combination of letters that constitute a discreet sound of speech in a language.

  • SpeakProgress. Raised each time the synthesizer completes speaking a word.

  • VisemeReached. Raised each time spoken output requires a change in the position of the mouth or the facial muscles used to produce speech.

  • BookmarkReached. Raised when the synthesizer encounters a bookmark in a prompt.

  • VoiceChange. Raised when the speaking voice for the synthesizer changes.

The SpeechSynthesizer does not raise the SpeakCompleted event while processing any of the Speak methods.

Speak(Prompt)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

Synchronously speaks the contents of a Prompt object.

public:
 void Speak(System::Speech::Synthesis::Prompt ^ prompt);
public void Speak (System.Speech.Synthesis.Prompt prompt);
member this.Speak : System.Speech.Synthesis.Prompt -> unit
Public Sub Speak (prompt As Prompt)

Parameters

prompt
Prompt

The content to speak.

Examples

The following example creates a Prompt object from a string and passes the object as an argument to the Speak method.

using System;
using System.Speech.Synthesis;

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

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

        // Configure the audio output.
        synth.SetOutputToDefaultAudioDevice();

        // Create a prompt from a string.
        Prompt color = new Prompt("What is your favorite color?");

        // Speak the contents of the prompt synchronously.
        synth.Speak(color);
      }

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

Remarks

To asynchronously speak the contents of a Prompt object, use SpeakAsync.

Applies to

Speak(PromptBuilder)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

Synchronously speaks the contents of a PromptBuilder object.

public:
 void Speak(System::Speech::Synthesis::PromptBuilder ^ promptBuilder);
public void Speak (System.Speech.Synthesis.PromptBuilder promptBuilder);
member this.Speak : System.Speech.Synthesis.PromptBuilder -> unit
Public Sub Speak (promptBuilder As PromptBuilder)

Parameters

promptBuilder
PromptBuilder

The content to speak.

Examples

The following example creates a PromptBuilder object from a string and passes the object as an argument to the Speak method.

using System;
using System.Speech.Synthesis;

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

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

        // Configure the audio output.
        synth.SetOutputToDefaultAudioDevice();

        // Create a PromptBuilder object and append a text string.
        PromptBuilder song = new PromptBuilder();
        song.AppendText("Say the name of the song you want to hear");

        // Speak the contents of the prompt synchronously.
        synth.Speak(song);
      }

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

Remarks

To asynchronously speak the contents of a PromptBuilder object, use SpeakAsync.

Applies to

Speak(String)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

Synchronously speaks the contents of a string.

public:
 void Speak(System::String ^ textToSpeak);
public void Speak (string textToSpeak);
member this.Speak : string -> unit
Public Sub Speak (textToSpeak As String)

Parameters

textToSpeak
String

The text to speak.

Examples

As shown in the following example, the Speak method provides the simplest means to generate speech output synchronously.

using System;
using System.Speech.Synthesis;

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

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

        // Configure the audio output.
        synth.SetOutputToDefaultAudioDevice();

        // Speak a string synchronously.
        synth.Speak("What is your favorite color?");
      }

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

Remarks

To synchronously speak a string that contains SSML markup, use the SpeakSsml method. To asynchronously speak the contents of a string, use the SpeakAsync method.

Applies to