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)

以非同步方式讀出 Prompt 物件的內容。Asynchronously speaks the contents of a Prompt object.

public:
 void SpeakAsync(System::Speech::Synthesis::Prompt ^ prompt);
public void SpeakAsync (System.Speech.Synthesis.Prompt prompt);
member this.SpeakAsync : System.Speech.Synthesis.Prompt -> unit

參數

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物件的內容, 請使用。 SpeakTo 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

參數

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物件的內容, 請使用。 SpeakTo 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.

另請參閱

適用於