SpeechSynthesizer.Speak 方法

定義

以同步方式從字串、Prompt 物件或 PromptBuilder 物件產生語音輸出。

多載

Speak(Prompt)

以同步方式讀出 Prompt 物件的內容。

Speak(PromptBuilder)

以同步方式讀出 PromptBuilder 物件的內容。

Speak(String)

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

備註

方法 Speak 會以同步方式產生語音。 方法在完整讀出 實例的內容 Speak 之前,不會傳回 。 這是產生語音的最簡單方式。 不過,如果您的應用程式需要在說話時執行工作,例如醒目提示文字、繪製動畫、監視控制項或其他工作,請使用 SpeakAsync 方法或 SpeakSsmlAsync 方法以非同步方式產生語音。

在呼叫這個方法期間, SpeechSynthesizer 可以引發下列事件:

  • StateChanged. 當合成器的說話狀態變更時引發。

  • SpeakStarted. 合成器開始產生語音時引發。

  • PhonemeReached. 每次合成器到達字母或字母組合時引發,這些字母會構成語言中的不離散語音音效。

  • SpeakProgress. 每次合成器完成說出單字時引發。

  • VisemeReached. 每次說出輸出都需要變更口氣的位置,或用來產生語音的臉部神經。

  • BookmarkReached. 當合成器在提示中遇到書簽時引發。

  • VoiceChange. 當合成器的說話語音變更時引發。

處理 SpeechSynthesizer 任何 Speak 方法時,不會引發 SpeakCompleted 事件。

Speak(Prompt)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

以同步方式讀出 Prompt 物件的內容。

public:
 void Speak(System::Speech::Synthesis::Prompt ^ prompt);
public void Speak (System.Speech.Synthesis.Prompt prompt);
member this.Speak : System.Speech.Synthesis.Prompt -> unit
Public Sub Speak (prompt As Prompt)

參數

prompt
Prompt

要讀出的內容。

範例

下列範例會 Prompt 從字串建立 物件,並將 物件當做引數傳遞至 Speak 方法。

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      using (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 synchronously.
        synth.Speak(color);
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}

備註

若要以非同步方式讀出 物件的內容 Prompt ,請使用 SpeakAsync

適用於

Speak(PromptBuilder)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

以同步方式讀出 PromptBuilder 物件的內容。

public:
 void Speak(System::Speech::Synthesis::PromptBuilder ^ promptBuilder);
public void Speak (System.Speech.Synthesis.PromptBuilder promptBuilder);
member this.Speak : System.Speech.Synthesis.PromptBuilder -> unit
Public Sub Speak (promptBuilder As PromptBuilder)

參數

promptBuilder
PromptBuilder

要讀出的內容。

範例

下列範例會 PromptBuilder 從字串建立 物件,並將 物件當做引數傳遞至 Speak 方法。

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      using (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 synchronously.
        synth.Speak(song);
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}

備註

若要以非同步方式讀出 物件的內容 PromptBuilder ,請使用 SpeakAsync

適用於

Speak(String)

Source:
SpeechSynthesizer.cs
Source:
SpeechSynthesizer.cs

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

public:
 void Speak(System::String ^ textToSpeak);
public void Speak (string textToSpeak);
member this.Speak : string -> unit
Public Sub Speak (textToSpeak As String)

參數

textToSpeak
String

要讀出的文字。

範例

如下列範例所示, Speak 方法提供最簡單的方法,以同步方式產生語音輸出。

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      using (SpeechSynthesizer synth = new SpeechSynthesizer())
      {

        // Configure the audio output.
        synth.SetOutputToDefaultAudioDevice();

        // Speak a string synchronously.
        synth.Speak("What is your favorite color?");
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}

備註

若要以同步方式說出包含 SSML 標記的 SpeakSsml 字串,請使用 方法。 若要以非同步方式讀出字串的內容,請使用 SpeakAsync 方法。

適用於