Share via


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)

來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs

以非同步方式讀出 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)

來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs

以非同步方式讀出 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)

來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs
來源:
SpeechSynthesizer.cs

以非同步方式讀出字串的內容。

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) 例外狀況。

另請參閱

適用於