Κοινή χρήση μέσω


SpeechSynthesizer.SpeakAsync Method

Definition

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

Overloads

SpeakAsync(Prompt)

Asynchronously speaks the contents of a Prompt object.

SpeakAsync(PromptBuilder)

Asynchronously speaks the contents of a PromptBuilder object.

SpeakAsync(String)

Asynchronously speaks the contents of a string.

Remarks

The SpeakAsync methods generate speech asynchronously. The methods return immediately without waiting for the content of the SpeakAsync object to finish speaking. Use SpeakAsync if your application needs to perform tasks while speaking, for example highlight text, paint animation, monitor controls, or other tasks.

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.

  • SpeakCompleted. Raised when the synthesizer finishes a SpeakAsync operation.

If your application does not need to perform tasks while speaking, you can use the Speak methods or the SpeakSsml method to generate speech synchronously.

SpeakAsync(Prompt)

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

Asynchronously speaks the contents of a Prompt object.

public:
 void SpeakAsync(System::Speech::Synthesis::Prompt ^ prompt);
public void SpeakAsync (System.Speech.Synthesis.Prompt prompt);
member this.SpeakAsync : System.Speech.Synthesis.Prompt -> unit
Public Sub SpeakAsync (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 SpeakAsync method.

using System;
using System.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.SetOutputToDefaultAudioDevice();

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

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

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

Remarks

You can cancel the asynchronous speaking of a prompt with the SpeakAsyncCancel or the SpeakAsyncCancelAll method.

To synchronously speak the contents of a Prompt object, use Speak.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Speak(Prompt).

Applies to

SpeakAsync(PromptBuilder)

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

Asynchronously speaks the contents of a PromptBuilder object.

public:
 System::Speech::Synthesis::Prompt ^ SpeakAsync(System::Speech::Synthesis::PromptBuilder ^ promptBuilder);
public System.Speech.Synthesis.Prompt SpeakAsync (System.Speech.Synthesis.PromptBuilder promptBuilder);
member this.SpeakAsync : System.Speech.Synthesis.PromptBuilder -> System.Speech.Synthesis.Prompt
Public Function SpeakAsync (promptBuilder As PromptBuilder) As Prompt

Parameters

promptBuilder
PromptBuilder

The content to speak.

Returns

The object that contains the content to speak.

Examples

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

using System;
using System.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.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 asynchronously.
      synth.SpeakAsync(song);

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

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Speak(PromptBuilder).

Remarks

To synchronously speak the contents of a PromptBuilder object, use Speak.

Applies to

SpeakAsync(String)

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

Asynchronously speaks the contents of a string.

public:
 System::Speech::Synthesis::Prompt ^ SpeakAsync(System::String ^ textToSpeak);
public System.Speech.Synthesis.Prompt SpeakAsync (string textToSpeak);
member this.SpeakAsync : string -> System.Speech.Synthesis.Prompt
Public Function SpeakAsync (textToSpeak As String) As Prompt

Parameters

textToSpeak
String

The text to speak.

Returns

The object that contains the content to speak.

Examples

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

using System;
using System.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.SetOutputToDefaultAudioDevice();

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

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

Remarks

To asynchronously speak a string that contains SSML markup, use the SpeakSsmlAsync method. To synchronously speak the contents of a string, use the Speak method. You can cancel the asynchronous speaking of a prompt with the SpeakAsyncCancel or the SpeakAsyncCancelAll method.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Speak(String).

See also

Applies to