Compartir a través de


Use Speech Synthesis Events

As a SpeechSynthesizer instance emits synthesized speech, it raises events for which applications can register event handlers. See the example at the end of this topic for more information.

Some events report the progress of the speech synthesizer in the speaking of a prompt. In ascending degrees of granularity, these events range from reporting each individual character or word processed, to features encountered in a prompt such as phonemes, visemes, and bookmarks, to the beginning and ending of the speaking of an entire prompt.

Other events report on the state of the speech synthesizer and on the changing of its speaking voice. The following table lists events on the SpeechSynthesizer class.

Event

Description

StateChanged

Raised when the state of the SpeechSynthesizer instance changes. The states are Paused, Ready, and Speaking.

SpeakStarted

Raised when a speak operation starts.

SpeakProgress

Raised after an individual word of a prompt is spoken.

BookmarkReached

Raised when a bookmark in a prompt is reached.

PhonemeReached

Raised when a phoneme boundary is reached.

VisemeReached

Raised when a viseme boundary is reached.

VoiceChange

Raised when the voice in use by the SpeechSynthesizer changes.

SpeakCompleted

Raised after a prompt is completely spoken.

Each of the events listed in the table is associated with a particular EventArgs class. Many of these associated classes contain properties named Cancelled, Error, and UserState that are inherited from the AsyncCompletedEventArgs class. These properties indicate, respectively, whether the particular asynchronous operation was cancelled, information about an error if one occurred, and an identifier for the asynchronous task that raised the event. In addition to these common properties, some of the event argument classes contain specialized properties, which are described under the following headings.

StateChanged

The synthesizer raises the StateChanged event when its state changes. This event is associated with the StateChangedEventArgs class, which has properties that indicate the synthesizer’s previous state (PreviousState) and its state after the change (State). The possible states (Paused, Ready, and Speaking) are members of the SynthesizerState enumeration.

SpeakStarted

The speech synthesizer automatically raises the SpeakStarted event when the synthesizer begins speaking the prompt it has synthesized. This event is associated with the SpeakStartedEventArgs class. The Prompt property on this class contains the prompt that caused the SpeakStarted event to be raised.

SpeakProgress

The synthesizer raises the SpeakProgress event periodically after the synthesized prompt has begun playing. These events are triggered as the speech synthesizer processes individual words in a prompt. This event is associated with the SpeakProgressEventArgs class, contains the following properties that provide information about the event: AudioPosition, CharacterCount, CharacterPosition, and Text.

BookmarkReached

You can insert a bookmark into a prompt using the AppendBookmark(String) method, or into prompts containing markup that conforms to the Speech Synthesis Markup Language (SSML) Version 1.0, using the mark element. A synthesizer will raise the BookmarkReached event when it encounters the bookmark. This event is associated with the BookmarkReachedEventArgs class, which has properties that store the position in the audio stream (AudioPosition) and the name of the bookmark (Bookmark). For more information, see Use Bookmarks.

PhonemeReached

The synthesizer raises the PhonemeReached event when it crosses a phoneme boundary in a prompt. The PhonemeReachedEventArgs class contains properties that provide information about the event, including the position of the phoneme in the audio stream (AudioPosition), how much time elapsed while speaking the phoneme (Duration), the speaking Emphasis given to the phoneme, the value of the Phoneme that triggered the event, and the value of the NextPhoneme. For more information, see Use Phonemes and Visemes.

VisemeReached

The synthesizer raises the VisemeReached event when it crosses a viseme boundary in a prompt. This event is associated with the VisemeReachedEventArgs class. This class contains properties that provide information about the event, such as the position of the viseme in the audio stream (AudioPosition), how much time elapsed while the viseme was processed (Duration), the speaking Emphasis given to the viseme, the value of the Viseme that triggered the event, and the value of the NextViseme. For more information, see Use Phonemes and Visemes.

VoiceChange

The synthesizer raises the VoiceChange event when the voice used for synthesis (the speaking voice) changes. This event is associated with the VoiceChangeEventArgs class. The only property of this class contains a VoiceInfo object, which provides information about the new voice, including the age, culture, and gender of the speaker. For more information, see Control Voice Attributes.

SpeakCompleted

The synthesizer automatically raises the SpeakCompleted event when it finishes speaking a synthesized prompt initiated with any of the SpeakAsync() or SpeakSsmlAsync(String) methods. The synthesizer does not raise this event for synchronous speak operations. This event is associated with the SpeakCompletedEventArgs class. The Prompt property on this class contains the prompt that caused the SpeakCompleted event to be raised. See the example for a typical use of this event.

Examples

The following example shows a console application that creates a SpeechSynthesizer instance named synth. The example then registers event handlers for the StateChanged, SpeakStarted, SpeakProgress, VoiceChange, and SpeakCompleted events. The example then creates basic code that demonstrates some of the information that is available in the handlers for these events.

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

      // Register for events.
      synth.StateChanged += new EventHandler<StateChangedEventArgs>(synth_StateChanged);
      synth.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(synth_SpeakStarted);
      synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);
      synth.VoiceChange += new EventHandler<VoiceChangeEventArgs>(synth_VoiceChange);
      synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);

      // Speak the prompt.
      synth.SpeakAsync("Green is my garden.");

      // Keep the console window open.
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }

    // Write the current state of the synthesizer to the console.
    static void synth_StateChanged(object sender, StateChangedEventArgs e)
    {
      Console.WriteLine("Current state of the synthesizer: " + e.State);
    }

    // Write a notification to the console when the speak operation starts.
    static void synth_SpeakStarted(object sender, SpeakStartedEventArgs e)
    {
      Console.WriteLine("Speak operation started");
    }

    // Write the current word being spoken to the console.
    static void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
    {
      Console.WriteLine("Current word being spoken: " + e.Text);
    }

    // Write the name of the new voice to the console.
    static void synth_VoiceChange(object sender, VoiceChangeEventArgs e)
    {
      Console.WriteLine("Name of the new voice: " + e.Voice.Name);
    }

    // Write a notification to the console when the speak operation completes.
    static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
    {
      Console.WriteLine("Speak operation completed");
    }
  }
}