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!
Exceptions Using Microsoft.Speech SpeechSynthesizer
-
29-05-2022 - |
Question
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.
La solution 2
Autres conseils
It takes me some time but i realized that i installed only MSSpeech_SR_en-US_TELE.msi which means SpeechRecognition. You need to scroll down in installer and install also text to speech e.g. "MSSpeech_TTS_en-US_Helen.msi".
I was having the same problem and noticed that it was a first-time-run problem. So what I did to solve this, is I have a List<InstalledVoice> InstalledVoices;
declared as a global property.
Then in the Form.Load(), I have this:
while (InstalledVoices == null)
{
InstalledVoices = SpeechSynth.GetInstalledVoices().ToList();
}
When I ran the debug output on that, it failed once, then succeeded the second time.
That guarantees that you have a collection of the Installed Voices and no null reference. SpeechSynth is my instance of the SpeechSynthesizer
class. I store each InstalledVoice
in a Dictionary<string, VoiceInfo>
for later reference.
Make sure you that Windows updates have been installed.
I tried to get away with a Windows 7 installation without any updates, and something like SpeechSynthesizer.SelectVoice(SomeVoiceName) would fail.
The only solution was to get the automatic Windows updates. Not sure which update exactely resolved the issue.
But I stumble over this problem again and again when I test my app in a VM with Windows 7 without updates.