ASP.NET 中的 SpeechSynthesizer - 异步错误
我希望能够通过调用 speak.aspx?text=Hello%20world
在我的 ASP.NET 应用程序中生成语音。这将给出 .wav
格式的响应。
到目前为止,我有一个空白页面,后面有代码:
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();
}
}
但是失败并显示消息:
InvalidOperationException:在此上下文中不允许异步操作。启动异步操作的页面必须将 Async 属性设置为 true,并且异步操作只能在 PreRenderComplete 事件之前的页面上启动。
如果我将 Async="true"
添加到 @Page
指令,代码会运行,但页面请求会无限期挂起。请让我知道出了什么问题,并显示正确的代码/使用方法?
请注意,我不能只使用 Google 文本转语音 API,因为它只允许使用 100 个或更少字符的字符串。
谢谢。
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 have 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该将上面的代码移至 page_load 方法中。没有真正的理由在预渲染中做你正在做的事情。
如果您使页面异步,那么您需要更改编程风格。看看这是否有帮助:
异步示例ASP.net Webforms (.NET 2.0) 中的页面处理
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)