Compartilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SpeechSynthesizer.SelectVoice Method

Selects a specific voice by name.

Namespace:  Microsoft.Speech.Synthesis
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Sub SelectVoice ( _
    name As String _
)
'Usage
Dim instance As SpeechSynthesizer
Dim name As String

instance.SelectVoice(name)
public void SelectVoice(
    string name
)

Parameters

Remarks

Use the GetInstalledVoices method and VoiceInfo class to obtain the names of installed text-to-speech (TTS) voices that you can select. To select a voice, pass the entire contents of the Name property as the argument for the SelectVoice(String) method. The SpeechSynthesizer object selects the first installed voice that contains name in the voice's VoiceInfo.Name property. The SpeechSynthesizer performs a case-sensitive, substring comparison to determine if the voice matches the name.

Typically, you will select a voice whose Culture property matches the culture parameter that is specified in the content to speak. You can specify a culture parameter for content to be spoken using the PromptBuilder(CultureInfo) constructor or the StartParagraph(CultureInfo) or StartSentence(CultureInfo) method. If authoring in Speech Synthesis Markup Language (SSML), you specify the culture using the xml:lang attribute.

When an application calls GetInstalledVoices(), the method verifies that each of the voices it finds in the registry meets certain minimum criteria. For any voice that fails verification, GetInstalledVoices() sets its Enabled property to False. An application cannot select a voice whose Enabled property is False. Typically, applications will not set a voice’s Enabled property.

To select a voice by gender, age, or locale, use one of the SelectVoiceByHints methods.

Examples

The following example uses the GetInstalledVoices() method and the VoiceInfo class to obtain the names of the installed text-to-speech voices.

using System;
using Microsoft.Speech.Synthesis;

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

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

        // Output information about all of the installed voices. 
        Console.WriteLine("Installed voices -");
        foreach (InstalledVoice voice in synth.GetInstalledVoices())
        {
          VoiceInfo info = voice.VoiceInfo;
          Console.WriteLine(" Voice Name: " + info.Name);
        }
      }

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

You can use one of the voice names discovered with the above example as an argument to the SelectVoice(String) method to select a voice to use. The following example selects the US English voice.

using System;
using Microsoft.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.SetOutputToWaveFile(@"C:\temp\test.wav");

        // Select the US English voice.
        synth.SelectVoice("Microsoft Server Speech Text to Speech Voice (en-US, Helen)");

        // Build a prompt.
        PromptBuilder builder = new PromptBuilder();
        builder.AppendText("That is a big pizza!");

        // Speak the prompt.
        synth.Speak(builder);
      }          
    }
  }
}

See Also

Reference

SpeechSynthesizer Class

SpeechSynthesizer Members

Microsoft.Speech.Synthesis Namespace