Use Speech Synthesis Events (Microsoft.Speech)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

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

BookmarkReached

Raised when a bookmark in a prompt is reached.

SpeakCompleted

Raised after a prompt is completely spoken.

SpeakProgress

Raised after an individual word of a prompt is spoken.

SpeakStarted

Raised when a prompt is first spoken.

StateChanged

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

VoiceChange

Raised when the voice in use by the SpeechSynthesizer changes.

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.

BookmarkReached

You can insert a bookmark into a prompt using the AppendBookmark(String) method. 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 (Microsoft.Speech).

SpeakCompleted

The synthesizer automatically raises the SpeakCompleted event when it finishes speaking the synthesized prompt. 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.

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, which contains the following properties that provide information about the prompt: AudioPosition, CharacterCount, CharacterPosition, Prompt, and Text.

SpeakStarted

The 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.

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.

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 (Microsoft.Speech).

Examples

The following example shows a method that creates a SpeechSynthesizer instance named synth. The method then registers event handlers for the SpeakStarted, SpeakProgress, SpeakCompleted, and BookmarkReached events. Following this method are simple event handlers for these events. The variable synth is a SpeechSynthesizer instance that is assumed to be declared at the class level.

C#  Copy imageCopy Code
private void Form1_Load(object sender, EventArgs e)
{
  synth = new SpeechSynthesizer();
  synth.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(synth_SpeakStarted);
  synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);
  synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);
  synth.BookmarkReached += new EventHandler<BookmarkReachedEventArgs>(synth_BookmarkReached);
}

C#  Copy imageCopy Code
void synth_SpeakStarted(object sender, SpeakStartedEventArgs e)
{
  AddEventText("Speak started");
}

void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
  AddEventText("Speak progress: " + e.Text);

  if (e.CharacterPosition + e.CharacterCount <= textBox.Text.Length &&
        textBox.Text.Substring(e.CharacterPosition, e.CharacterCount) == e.Text)
  {
    textBox.Select(e.CharacterPosition, e.CharacterCount);
  }
}

void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
  AddEventText("Speak completed");

  textBox.SelectAll();
  textBox.Focus();
}


void synth_BookmarkReached(object sender, BookmarkReachedEventArgs e)
{
  AddEventText("Bookmark reached: " + e.Bookmark);
}