SpeechSynthesizer.SpeakAsync 方法

定义

从字符串、 Prompt 对象或 PromptBuilder 对象异步生成语音输出。

重载

名称 说明
SpeakAsync(Prompt)

以异步方式表示对象的内容 Prompt

SpeakAsync(PromptBuilder)

以异步方式表示对象的内容 PromptBuilder

SpeakAsync(String)

以异步方式表示字符串的内容。

注解

这些 SpeakAsync 方法异步生成语音。 这些方法会立即返回,而无需等待对象的内容 SpeakAsync 完成说话。 如果你的应用程序需要在说话时执行任务,例如突出显示文本、画图动画、监视控件或其他任务,请使用 SpeakAsync

在调用此方法期间, SpeechSynthesizer 可以引发以下事件:

  • StateChanged。 合成器说话状态更改时引发。

  • SpeakStarted。 合成器开始生成语音时引发。

  • PhonemeReached。 每次合成器到达一个字母或字母组合时引发,这些字母构成语言中谨慎的语音声音。

  • SpeakProgress。 每次合成器完成说话时引发。

  • VisemeReached。 每次说出输出时引发都需要改变用于生成语音的嘴或面部肌肉的位置。

  • BookmarkReached。 当合成器在提示中遇到书签时引发。

  • VoiceChange。 合成器语音更改时引发。

  • SpeakCompleted。 合成器完成 SpeakAsync 操作时引发。

如果应用程序不需要在说话时执行任务,则可以使用 Speak 方法或 SpeakSsml 方法同步生成语音。

SpeakAsync(Prompt)

以异步方式表示对象的内容 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

要说的内容。

示例

以下示例从字符串创建一个 Prompt 对象,并将该对象作为参数传递给 SpeakAsync 该方法。

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

注解

可以使用或SpeakAsyncCancelAll方法取消提示的异步说话SpeakAsyncCancel

若要同步朗讲对象的内容 Prompt ,请使用 Speak

此方法存储在任务中,它返回该方法的同步对应项可以引发的所有非使用异常。 如果异常存储在返回的任务中,则等待任务时将引发该异常。 使用情况异常(例如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅由 Speak(Prompt)..

适用于

SpeakAsync(PromptBuilder)

以异步方式表示对象的内容 PromptBuilder

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

要说的内容。

返回

包含要说话的内容的对象。

示例

以下示例从字符串创建一个 PromptBuilder 对象,并将该对象作为参数传递给 SpeakAsync 该方法。

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

此方法存储在任务中,它返回该方法的同步对应项可以引发的所有非使用异常。 如果异常存储在返回的任务中,则等待任务时将引发该异常。 使用情况异常(例如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅由 Speak(PromptBuilder)..

注解

若要同步朗讲对象的内容 PromptBuilder ,请使用 Speak

适用于

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

要说的文本。

返回

包含要说话的内容的对象。

示例

如以下示例所示,该方法 SpeakAsync 提供了异步生成语音输出的最简单方法。

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 该方法。 若要同步朗讲字符串的内容,请使用 Speak 该方法。 可以使用或SpeakAsyncCancelAll方法取消提示的异步说话SpeakAsyncCancel

此方法存储在任务中,它返回该方法的同步对应项可以引发的所有非使用异常。 如果异常存储在返回的任务中,则等待任务时将引发该异常。 使用情况异常(例如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅由 Speak(String)..

另请参阅

适用于