Partager via


Note

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

SpeechSynthesizer.SpeakAsync Method (String)

Asynchronously speaks the contents of a string.

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

Syntax

'Declaration
Public Function SpeakAsync ( _
    textToSpeak As String _
) As Prompt
'Usage
Dim instance As SpeechSynthesizer
Dim textToSpeak As String
Dim returnValue As Prompt

returnValue = instance.SpeakAsync(textToSpeak)
public Prompt SpeakAsync(
    string textToSpeak
)

Parameters

Return Value

Type: Microsoft.Speech.Synthesis.Prompt
Returns the object that contains the content to speak.

Remarks

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

Examples

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

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

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

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

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

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

See Also

Reference

SpeechSynthesizer Class

SpeechSynthesizer Members

SpeakAsync Overload

Microsoft.Speech.Synthesis Namespace