Partager via


Construct and Speak a Simple Prompt

For more control over how a phrase is spoken, create a prompt using a constructor on the PromptBuilder class. After you create a PromptBuilder instance with its empty prompt, you can append text, audio, Speech Synthesis Markup Language (SSML) markup, a bookmark, a break, or an existing PromptBuilder instance to the one you just created. After all of the prompt elements are in place, the prompt can be played.

Creating a PromptBuilder Instance

You can create a PromptBuilder instance by calling a PromptBuilder constructor. The constructor that takes no arguments, PromptBuilder(), creates a PromptBuilder instance using the current language-culture setting of the computer, for example en-US. You can use the other constructor, PromptBuilder(CultureInfo), to set the culture to use for speaking the prompt. To perform text-to-speech using the language specified in the CultureInfo argument, a speech synthesis engine that supports that language-country code must be installed.

The following example creates a PromptBuilder instance, and then appends the string "Welcome, everyone" to the prompt.

PromptBuilder pb = new PromptBuilder();
pb.AppendText("Welcome, everyone");

Playing a Prompt

After a prompt is created, it can be played by calling the Speak(PromptBuilder) or SpeakAsync(PromptBuilder) methods on the SpeechSynthesizer instance. In the following example, a SpeechSynthesizer instance named synth has previously been created.

PromptBuilder pb = new PromptBuilder();
pb.AppendText("Welcome, everyone");
synth.Speak(pb);