开发者

SpeechSynthesizer in ASP.NET - async error

开发者 https://www.devze.com 2023-04-10 22:52 出处:网络
I would like to be able to generate speech in my ASP.NET app by calling speak.aspx?text=Hello%20world. This would give a response in .wav format.

I would like to be able to generate speech in my ASP.NET app by calling speak.aspx?text=Hello%20world. This would give a response in .wav format.

So far I have a blank page with code behind:

protected void Page_PreRender(object sender, EventArgs e)
{
  using (var ss = new SpeechSynthesizer()) {
    MemoryStream str = new MemoryStream();
    ss.SetOutputToWaveStream(str);
    ss.Speak(Server.UrlDecode(Request.QueryString["text"]));
    Response.AddHeader("Content-Type", "audio/wav");
    str.WriteTo(Response.OutputStream);
    str.Close();
  }
}

However this fails with message:

InvalidOperationException: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to ha开发者_如何学运维ve the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.

If I add Async="true" to the @Page directive, the code runs but a request for the page hangs indefinitely. Please could you let me know what's wrong, and show the correct code/approach to use?

Note I can't just use the Google text-to-speech API since it only allows strings of 100 characters or less.

Thank you.


You should probably move the code above into the page_load method. there's no real reason for doing what you're doing in prerender.

If you make the page async, then you need to change your programming style. see if this helps:

Example of Asynchronous page processing in ASP.net webforms (.NET 2.0)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号