C# SpeechSynthesizer makes service unresponsive
Asked Answered
M

2

6

I have the folowing code

[WebMethod]
public byte[] stringToWav(string text)
{
    SpeechSynthesizer ss = new SpeechSynthesizer();
    MemoryStream ms = new MemoryStream();
    ss.SetOutputToWaveStream(ms);
    ss.Speak(text);
    return ms.ToArray();
}

and the service returns nothing. Any idea why this happens?

Mudd answered 12/1, 2011 at 16:24 Comment(3)
Do you mean it returns a 0-length byte array, or null?Citrine
it doesnt really return anything. Everytime I use speechSnythesis there is no return from service, even if I change return to a random string...Friedman
It really wouldn't surprise me to find that this API requires a desktop application.Ahmad
F
7

I ran into the same exact problem with an ashx page.

I don't understand exactly why, but it seems that you need to use a separate thread and wait for it to complete.

The following code worked for me:

public byte[] TextToBytes(string textToSpeak)
{
    byte[] byteArr = null;

    var t = new System.Threading.Thread(() =>
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        using (MemoryStream memoryStream = new MemoryStream())
        {
            ss.SetOutputToWaveStream(memoryStream);
            ss.Speak(textToSpeak);
            byteArr = memoryStream.ToArray();
        }
    });
    t.Start();
    t.Join();
    return byteArr;
}
Fleischer answered 8/4, 2011 at 19:35 Comment(0)
I
0

Have you debugged and checked the value of ms.ToArray()? You might have better luck with ms.ToByteArray().

Impassible answered 12/1, 2011 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.