Partager via


Note

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

SpeechSynthesizer.SpeakAsync Method (Prompt)

Asynchronously speaks the contents of a Prompt object.

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

Syntax

'Declaration
Public Sub SpeakAsync ( _
    prompt As Prompt _
)
'Usage
Dim instance As SpeechSynthesizer
Dim prompt As Prompt

instance.SpeakAsync(prompt)
public void SpeakAsync(
    Prompt prompt
)

Parameters

Remarks

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

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

Examples

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

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

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

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