1

In an MVC web application I use the SpeechSynthesizer class to speak some text to a .wav file during a function called by a controller action handler that returns a view. The code executes, writes the file, and the action handle returns, but the development server usually, but not always, never comes back with the return page. This is the text-to-speech code:

string threadMessage = null;
bool returnValue = true;

var t = new System.Threading.Thread(() =>
{
    try
    {
        SpeechEngine.SetOutputToWaveFile(wavFilePath);
        SpeechEngine.Speak(text);
        SpeechEngine.SetOutputToNull();
    }
    catch (Exception exception)
    {
        threadMessage = "Error doing text to speech to file: " + exception.Message;
        returnValue = false;
    }
});
t.Start();
t.Join();

if (!returnValue)
{
    message = threadMessage;
    return returnValue;
}

I saw a couple of posts for a similar problem in a service that advised doing the operation in a thread, hence the above thread.

Actually, using the SpeechSynthesizer for other things can hang as well. I had a page that just enumerated the voices, but it would get stuck as well. Since there is no user code in any of the threads if I pause the debugger, I have no clue how to debug it.

I've tried Dispose'ing the SpeechSynthesizer object afterwards, calling SetOutputToDefaultVoice, to no avail. I've tried it on both Windows 8.1 and Windows 8, running with the development server under the debugger, or running IIS Express separately.

Any ideas? Is there other information I could give that would be helpful?

Thanks.

-John

jtsoftware
  • 521
  • 3
  • 14
  • 1
    Why are you doing it in a thread in the first place? I tend not to just add threading to my code without a good reason. – mason May 23 '14 at 14:51
  • Why are you doing it in a separate thread, if you have to wait for the operation to complete before continuing? – Dominic Zukiewicz May 23 '14 at 14:51
  • I mentioned above that I added the thread because some other StackOverflow post suggested it, but there is no difference in the problem with or without the thread. I will remove the thread when I have a real fix, assuming it's not needed. – jtsoftware May 27 '14 at 19:10
  • Here are the responses I got from forums.asp.net: http://forums.asp.net/p/1989192/5709873.aspx?p=True&t=635373937182401839&pagenum=1 Apparently using the speech synthesizer is not allowed, but since it still seems a bit questionable, I'm leaving the question open for now. – jtsoftware Jun 03 '14 at 16:07
  • Possible duplicate of [Creating a ASP.NET application converting text to speech](http://stackoverflow.com/questions/1716447/creating-a-asp-net-application-converting-text-to-speech) – Nikolay Shmyrev Dec 05 '16 at 19:14

1 Answers1

1

Try

Public void Speak(string wavFilePath, string text)
{
        using (var synthesizer = new SpeechSynthesizer())
        {
            synthesizer.SetOutputToWaveFile(wavFilePath);
            synthesizer.Speak(text);
            return outputFile;
        }
}

Task.Run(() => Speak("path", "text")).Result;

It worked for me in IIS Express

ozba
  • 6,522
  • 4
  • 33
  • 40