C# SpeechSynthesizer makes service unresponsive

只谈情不闲聊 提交于 2019-11-27 07:47:53

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/4671158/c-sharp-speechsynthesizer-makes-service-unresponsive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!