Export (0) Print
Expand All

SpeechSynthesizer.SpeakAsync Method (PromptBuilder)

Asynchronously speaks the contents of a PromptBuilder object.

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

public Prompt SpeakAsync(
	PromptBuilder promptBuilder
)

Parameters

promptBuilder
Type: Microsoft.Speech.Synthesis.PromptBuilder
The content to speak.

Return Value

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

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

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

Show:
© 2015 Microsoft