【问题标题】:SpeechSynthesizer in ASP.NET - async errorASP.NET 中的 SpeechSynthesizer - 异步错误
【发布时间】:2011-10-06 12:53:07
【问题描述】:

我希望能够通过调用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 个字符或更少的字符串。

谢谢。

【问题讨论】:

    标签: .net asp.net asynchronous text-to-speech speechsynthesizer


    【解决方案1】:

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

    如果你让页面异步,那么你需要改变你的编程风格。看看这是否有帮助:

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

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多