SpeechSynthesizer.SpeakAsync 方法
定义
从字符串、 Prompt 对象或 PromptBuilder 对象异步生成语音输出。Generates speech output asynchronously from a string, a Prompt object, or a PromptBuilder object.
重载
| SpeakAsync(Prompt) |
异步使用 Prompt 对象内容的语言。Asynchronously speaks the contents of a Prompt object. |
| SpeakAsync(PromptBuilder) |
异步使用 PromptBuilder 对象内容的语言。Asynchronously speaks the contents of a PromptBuilder object. |
| SpeakAsync(String) |
异步使用字符串内容的语言。Asynchronously speaks the contents of a string. |
注解
SpeakAsync方法以异步方式生成语音。The SpeakAsync methods generate speech asynchronously. 方法会立即返回,而不会等待对象的内容 SpeakAsync 完成说话。The methods return immediately without waiting for the content of the SpeakAsync object to finish speaking. SpeakAsync如果你的应用程序需要在说话时执行任务,例如突出显示文本、绘制动画、监视器控件或其他任务,请使用。Use SpeakAsync if your application needs to perform tasks while speaking, for example highlight text, paint animation, monitor controls, or other tasks.
在调用此方法的过程中, SpeechSynthesizer 可能会引发以下事件:During a call to this method, the SpeechSynthesizer can raise the following events:
StateChanged.StateChanged. 合成器的讲述状态更改时引发。Raised when the speaking state of the synthesizer changes.
SpeakStarted.SpeakStarted. 合成器开始生成语音时引发。Raised when the synthesizer begins generating speech.
PhonemeReached.PhonemeReached. 每次合成器达到一种语言的字母或字母组合时引发。Raised each time the synthesizer reaches a letter or combination of letters that constitute a discreet sound of speech in a language.
SpeakProgress.SpeakProgress. 每次合成器完成字词时引发。Raised each time the synthesizer completes speaking a word.
VisemeReached.VisemeReached. 每次出现口述输出时,需要更改嘴或用于生成语音的面部肌肉的位置。Raised each time spoken output requires a change in the position of the mouth or the facial muscles used to produce speech.
BookmarkReached.BookmarkReached. 当合成器在提示符处遇到书签时引发。Raised when the synthesizer encounters a bookmark in a prompt.
VoiceChange.VoiceChange. 合成器的说话声音发生变化时引发。Raised when the speaking voice for the synthesizer changes.
SpeakCompleted.SpeakCompleted. 合成器完成操作时引发 SpeakAsync 。Raised when the synthesizer finishes a SpeakAsync operation.
如果你的应用程序不需要在说话时执行任务,则可以使用 Speak 方法或 SpeakSsml 方法同步生成语音。If your application does not need to perform tasks while speaking, you can use the Speak methods or the SpeakSsml method to generate speech synchronously.
SpeakAsync(Prompt)
public:
void SpeakAsync(System::Speech::Synthesis::Prompt ^ prompt);
public void SpeakAsync (System.Speech.Synthesis.Prompt prompt);
member this.SpeakAsync : System.Speech.Synthesis.Prompt -> unit
Public Sub SpeakAsync (prompt As Prompt)
参数
- prompt
- Prompt
要朗读的内容。The content to speak.
示例
下面的示例 Prompt 从字符串创建对象并将该对象作为参数传递给 SpeakAsync 方法。The following example creates a Prompt object from a string and passes the object as an argument to the SpeakAsync method.
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();
// Create a prompt from a string.
Prompt color = new Prompt("What is your favorite color?");
// Speak the contents of the prompt asynchronously.
synth.SpeakAsync(color);
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
注解
您可以使用或方法取消对提示的异步处理 SpeakAsyncCancel SpeakAsyncCancelAll 。You can cancel the asynchronous speaking of a prompt with the SpeakAsyncCancel or the SpeakAsyncCancelAll method.
若要同步朗读对象的内容 Prompt ,请使用 Speak 。To synchronously speak the contents of a Prompt object, use Speak.
适用于
SpeakAsync(PromptBuilder)
异步使用 PromptBuilder 对象内容的语言。Asynchronously speaks the contents of a PromptBuilder object.
public:
System::Speech::Synthesis::Prompt ^ SpeakAsync(System::Speech::Synthesis::PromptBuilder ^ promptBuilder);
public System.Speech.Synthesis.Prompt SpeakAsync (System.Speech.Synthesis.PromptBuilder promptBuilder);
member this.SpeakAsync : System.Speech.Synthesis.PromptBuilder -> System.Speech.Synthesis.Prompt
Public Function SpeakAsync (promptBuilder As PromptBuilder) As Prompt
参数
- promptBuilder
- PromptBuilder
要朗读的内容。The content to speak.
返回
返回包含要使用的内容的对象。Returns the object that contains the content to speak.
示例
下面的示例 PromptBuilder 从字符串创建对象并将该对象作为参数传递给 SpeakAsync 方法。The following example creates a PromptBuilder object from a string and passes the object as an argument to the SpeakAsync method.
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();
// 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();
}
}
}
注解
若要同步朗读对象的内容 PromptBuilder ,请使用 Speak 。To synchronously speak the contents of a PromptBuilder object, use Speak.
适用于
SpeakAsync(String)
异步使用字符串内容的语言。Asynchronously speaks the contents of a string.
public:
System::Speech::Synthesis::Prompt ^ SpeakAsync(System::String ^ textToSpeak);
public System.Speech.Synthesis.Prompt SpeakAsync (string textToSpeak);
member this.SpeakAsync : string -> System.Speech.Synthesis.Prompt
Public Function SpeakAsync (textToSpeak As String) As Prompt
参数
- textToSpeak
- String
要朗读的文本。The text to speak.
返回
返回包含要使用的内容的对象。Returns the object that contains the content to speak.
示例
如下面的示例中所示, SpeakAsync 方法提供了异步生成语音输出的最简单方法。As shown in the following example, the SpeakAsync method provides the simplest means to generate speech output asynchronously.
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();
// Speak a string asynchronously.
synth.SpeakAsync("What is your favorite color?");
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
注解
若要以异步方式口述包含 SSML 标记的字符串,请使用 SpeakSsmlAsync 方法。To asynchronously speak a string that contains SSML markup, use the SpeakSsmlAsync method. 若要同步朗读字符串的内容,请使用 Speak 方法。To synchronously speak the contents of a string, use the Speak method. 您可以使用或方法取消对提示的异步处理 SpeakAsyncCancel SpeakAsyncCancelAll 。You can cancel the asynchronous speaking of a prompt with the SpeakAsyncCancel or the SpeakAsyncCancelAll method.