SpeechSynthesizer.SpeakAsync SpeechSynthesizer.SpeakAsync SpeechSynthesizer.SpeakAsync SpeechSynthesizer.SpeakAsync Method

Definition

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

Overloads

SpeakAsync(Prompt) SpeakAsync(Prompt) SpeakAsync(Prompt)

Asynchronously speaks the contents of a Prompt object.

SpeakAsync(PromptBuilder) SpeakAsync(PromptBuilder) SpeakAsync(PromptBuilder)

Asynchronously speaks the contents of a PromptBuilder object.

SpeakAsync(String) SpeakAsync(String) SpeakAsync(String) 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) SpeakAsync(Prompt) SpeakAsync(Prompt)

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

Parameters

prompt
Prompt Prompt 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.

SpeakAsync(PromptBuilder) SpeakAsync(PromptBuilder) SpeakAsync(PromptBuilder)

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

Parameters

promptBuilder
PromptBuilder PromptBuilder PromptBuilder PromptBuilder

The content to speak.

Returns

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

Remarks

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

SpeakAsync(String) SpeakAsync(String) SpeakAsync(String) SpeakAsync(String)

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 String String String

The text to speak.

Returns

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.

See also

Applies to