SpeechSynthesizer.Speak 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从字符串、 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
- 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 -> unitPublic 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
- 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 -> unitPublic 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
- Source:
- SpeechSynthesizer.cs
同步使用字符串内容的语言。
public:
 void Speak(System::String ^ textToSpeak);public void Speak(string textToSpeak);member this.Speak : string -> unitPublic 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 方法。