如何在 ASP.NET MVC 中使用 windows 语音合成器
How to use windows speech synthesizer in ASP.NET MVC
我尝试使用 System.Speech
class 在 ASP.NET mvc 应用程序中生成语音。
[HttpPost]
public ActionResult TTS(string text)
{
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.Speak(text);
return View();
}
But it gives the following error.
System.InvalidOperationException: 'An asynchronous operation cannot be
Started at this time. Asynchronous operations may only be started within an
asynchronous handler or module or during certain events in the Page lifecycle.
If this exception occurred while executing a Page, ensure that the Page is
marked <%@ Page Async="true" %>.
This exception may also indicate an attempt to call an "async void" method,
which is generally unsupported within ASP.NET request processing. Instead,
the asynchronous method should return a Task, and the caller should await it.
我在 wpf 应用程序中使用了 System.Speech class 和异步方法。
System.Speech class 可以在 ASP.NET mvc 应用程序中使用吗?
怎么做?
-
<%@ Page Async="true" %>
应该在哪里
放置?
答案是:是,可以在MVC中使用System.Speech
class
我认为您可以尝试使用 async
控制器操作方法,并使用 SpeechSynthesizer.Speak
和 Task.Run
方法,如下所示:
[HttpPost]
public async Task<ActionResult> TTS(string text)
{
Task<ViewResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
speechSynthesizer.Speak(text);
return View();
}
});
return await task;
}
但是,如上例所示,生成的声音在服务器上播放,因为上面的代码运行在服务器端而不是客户端。要启用客户端播放,您可以使用 SetOutputToWaveFile
方法并在返回视图页面时使用 audio
标签播放音频内容,如下例所示(假设您使用 HTML 5 in CSHTML 视图):
控制器
[HttpPost]
public async Task<ActionResult> TTS(string text)
{
// you can set output file name as method argument or generated from text
string fileName = "fileName";
Task<ViewResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
speechSynthesizer.SetOutputToWaveFile(Server.MapPath("~/path/to/file/") + fileName + ".wav");
speechSynthesizer.Speak(text);
ViewBag.FileName = fileName + ".wav";
return View();
}
});
return await task;
}
查看
<audio autoplay="autoplay" src="@Url.Content("~/path/to/file/" + ViewBag.FileName)">
</audio>
或者您可以将操作类型更改为 FileContentResult
并使用 MemoryStream
和 SetOutputToWaveStream
让用户自己播放音频文件:
Task<FileContentResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
using (MemoryStream stream = new MemoryStream())
{
speechSynthesizer.SetOutputToWaveStream(stream);
speechSynthesizer.Speak(text);
var bytes = stream.GetBuffer();
return File(bytes, "audio/x-wav");
}
}
});
参考:
Using Asynchronous Methods in ASP.NET MVC
类似问题:
我尝试使用 System.Speech
class 在 ASP.NET mvc 应用程序中生成语音。
[HttpPost]
public ActionResult TTS(string text)
{
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.Speak(text);
return View();
}
But it gives the following error.
System.InvalidOperationException: 'An asynchronous operation cannot be
Started at this time. Asynchronous operations may only be started within an
asynchronous handler or module or during certain events in the Page lifecycle.
If this exception occurred while executing a Page, ensure that the Page is
marked <%@ Page Async="true" %>.
This exception may also indicate an attempt to call an "async void" method,
which is generally unsupported within ASP.NET request processing. Instead,
the asynchronous method should return a Task, and the caller should await it.
我在 wpf 应用程序中使用了 System.Speech class 和异步方法。
System.Speech class 可以在 ASP.NET mvc 应用程序中使用吗?
怎么做?
-
<%@ Page Async="true" %>
应该在哪里 放置?
答案是:是,可以在MVC中使用System.Speech
class
我认为您可以尝试使用 async
控制器操作方法,并使用 SpeechSynthesizer.Speak
和 Task.Run
方法,如下所示:
[HttpPost]
public async Task<ActionResult> TTS(string text)
{
Task<ViewResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
speechSynthesizer.Speak(text);
return View();
}
});
return await task;
}
但是,如上例所示,生成的声音在服务器上播放,因为上面的代码运行在服务器端而不是客户端。要启用客户端播放,您可以使用 SetOutputToWaveFile
方法并在返回视图页面时使用 audio
标签播放音频内容,如下例所示(假设您使用 HTML 5 in CSHTML 视图):
控制器
[HttpPost]
public async Task<ActionResult> TTS(string text)
{
// you can set output file name as method argument or generated from text
string fileName = "fileName";
Task<ViewResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
speechSynthesizer.SetOutputToWaveFile(Server.MapPath("~/path/to/file/") + fileName + ".wav");
speechSynthesizer.Speak(text);
ViewBag.FileName = fileName + ".wav";
return View();
}
});
return await task;
}
查看
<audio autoplay="autoplay" src="@Url.Content("~/path/to/file/" + ViewBag.FileName)">
</audio>
或者您可以将操作类型更改为 FileContentResult
并使用 MemoryStream
和 SetOutputToWaveStream
让用户自己播放音频文件:
Task<FileContentResult> task = Task.Run(() =>
{
using (SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer())
{
using (MemoryStream stream = new MemoryStream())
{
speechSynthesizer.SetOutputToWaveStream(stream);
speechSynthesizer.Speak(text);
var bytes = stream.GetBuffer();
return File(bytes, "audio/x-wav");
}
}
});
参考:
Using Asynchronous Methods in ASP.NET MVC
类似问题: