Microsoft.Speech.Synthesis does not work for Text

2019-02-19 05:46发布

问题:

I just try to run simple microsoft example for Text To Speech using using Microsoft.Speech.dll;

using System;
using Microsoft.Speech.Synthesis;

namespace TTS
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Testing TTS!");

            // 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);
                }

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

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

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

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

Although I have the right voices, it does not make any sound. No Text To Speech(TTS) voice.

When I use Microsoft System.Speech.dll then I can hear voice. So there is no sound problem.

using System;
using System.Speech.Synthesis;

namespace TTS
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Testing TTS!");

            // 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);
                }

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

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

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

Shortly

Why I can not hear any voice or make Text To Speech(TTS) with Microsoft Speech Platform using Microsoft.Speech? Should I do some extra config?

回答1:

Because you're using two different TTS engines. Microsoft.Speech uses the server TTS voices; System.Speech uses the desktop TTS voices. See the discussion here.

Windows Vista and above have desktop TTS voices registered by default, but no server TTS voices. When you install the Server Speech Platform Runtime, which I believe you have to do in order to get the Microsoft.Speech.dll loaded in the first place, you should have the option to install some server TTS voices as well.



回答2:

On Visual Studio Select Project, then select Add References, Then select the checkbox next to System.Speech

In your program do a using system.speech as well.

Works fine for me.



回答3:

To answer your question you asked:

Why I can not hear any voice or make Text To Speech(TTS) with Microsoft Speech Platform using Microsoft.Speech?

You are missing an important thing in your code. You cannot hear a voice because following line is missing:

synth.SetOutputToDefaultAudioDevice();

This allows you to hear the voice. I had the same problem. I modified your code an inserted the code sample above:

using System;

using System.Speech.Synthesis;
using Microsoft.Speech.Synthesis;
namespace ConsoleApplication1
{
 class Program
   {

    static void Main(string[] args)
    {
     Console.WriteLine("Testing TTS!");

     // Initialize a new instance of the SpeechSynthesizer.
     using (SpeechSynthesizer synth = new SpeechSynthesizer())
     {
        // Configure the synthesizer to send output to the default audio device.
        synth.SetOutputToDefaultAudioDevice();

        // 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);
        }

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

        // Speak.
        synth.Speak("That is a big pizza!");
     }

     Console.Write("Press any key to continue . . . ");
     Console.ReadKey(true);
  }
 }
}

Notice that the SetOutputToDefaultAudioDevice is not necessary when you use the System.Speech.dll. Here is the link to the documentation about the method.