Search code examples
c#text-to-speechspeech-synthesis

Exceptions Using Microsoft.Speech SpeechSynthesizer


I'm working on a speech synthesis project, and I decided to try and use the Microsoft.Speech namespace instead of the built-in System.Speech namespace because Microsoft isn't fixing the memory leak here and recommends using Microsoft.Speech as a workaround.

When I run the program below, I get a NullReferenceException when it calls GetInstalledVoices.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Speech.Synthesis;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.GetInstalledVoices();
        }
    }
}

And when I run this next program, I get a UnauthorizedAccessException (I am running as an administrator) when it calls Speak.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Speech.Synthesis;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.Speak("exception");
        }
    }
}

I'm running VS Express 2012 on Windows 8 x64, and the project is configured for x64. I installed the x64 runtime and SDK for Microsoft speech, and installed the en-us language pack from http://www.microsoft.com/en-us/download/details.aspx?id=27224. I even tried downloading the x86 runtime and SDK and changing my project to x86, but that results in a PlatformNotSupportedException.

Is there some other install I'm missing, or is the Microsoft.Speech namespace just not supported on my platform? If I change using Microsoft.Speech.Synthesis to using System.Speech.Synthesis, it's fine except for the memory leak that I mentioned, and I can probably get away with that for now, since this is a hobby application, not for work.


Solution

  • I'm using eSpeak instead, and just shelling out to their command line program from my .Net program. This is a better solution for me because eSpeak and it's associated voice are easy to install on multiple computers - if I used the Microsoft Speech solutions, I would be stuck with whatever the default voices on that computer are, unless we bought voices for each computer. It also happens that the robotic-sounding eSpeak voice is a better fit for my project, because guess what, it's a talking robot head!