ASP.NET中的SpeechSynthesizer-异步错误 [英] SpeechSynthesizer in ASP.NET - async error

查看:530
本文介绍了ASP.NET中的SpeechSynthesizer-异步错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过调用speak.aspx?text=Hello%20world在我的ASP.NET应用程序中生成语音.这样会以.wav格式给出响应.

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 :在此上下文中不允许异步操作.页面启动异步操作必须将Async属性设置为true,并且异步操作只能在PreRenderComplete事件之前的页面上启动.

InvalidOperationException: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.

如果将Async="true"添加到@Page指令,则代码将运行,但对页面的请求将无限期挂起.请您让我知道问题出在哪里,并显示要使用的正确代码/方法吗?

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?

请注意,我不能只使用Google文本语音转换API,因为它只允许100个字符或更少的字符串.

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

谢谢.

推荐答案

您可能应该将上面的代码移到page_load方法中.没有真正的理由在预渲染中做您正在做的事情.

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:

异步页面处理的示例在ASP.net网络表单(.NET 2.0)中

这篇关于ASP.NET中的SpeechSynthesizer-异步错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆