Partager via


Note

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

SpeechSynthesizer.SpeakAsync Method (PromptBuilder)

Asynchronously speaks the contents of a PromptBuilder object.

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

Syntax

'Declaration
Public Function SpeakAsync ( _
    promptBuilder As PromptBuilder _
) As Prompt
'Usage
Dim instance As SpeechSynthesizer
Dim promptBuilder As PromptBuilder
Dim returnValue As Prompt

returnValue = instance.SpeakAsync(promptBuilder)
public Prompt SpeakAsync(
    PromptBuilder promptBuilder
)

Parameters

Return Value

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

Remarks

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

Examples

The following example creates a PromptBuilder object from a string and passes the object as an argument to the SpeakAsync(PromptBuilder) 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\Song.wav");

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

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

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

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

See Also

Reference

SpeechSynthesizer Class

SpeechSynthesizer Members

SpeakAsync Overload

Microsoft.Speech.Synthesis Namespace