如何在ASP.NET MVC中使用Windows语音合成器 [英] How to use windows speech synthesizer in ASP.NET MVC

查看:154
本文介绍了如何在ASP.NET MVC中使用Windows语音合成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用System.Speech类在ASP.NET mvc应用程序中生成语音.

I tried to use the System.Speech class to generate speech in ASP.NET mvc application.

[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类和异步方法.

  1. 是否可以在ASP.NET mvc应用程序中使用System.Speech类?

  1. Can the System.Speech class be used in a ASP.NET mvc application?

如何做到?

推荐答案

答案是:,您可以在MVC中使用System.Speech类.

The answer is: yes, you can use System.Speech class in MVC.

我认为您可以尝试使用async控制器操作方法并使用

I think you can try using async controller action method and use SpeechSynthesizer.Speak with Task.Run method like this:

[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标签播放音频内容(假设您在CSHTML视图中使用HTML 5):

However, as in example above the generated sound plays on server, because the code above runs server-side instead of client-side. To enable playing on client-side, you can use SetOutputToWaveFile method and use audio tag to play audio content when returning view page shown in example below (assumed you're using HTML 5 in CSHTML view):

控制器

[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并在

Or you can change action type to FileContentResult and use MemoryStream with SetOutputToWaveStream to let user play audio file himself:

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");
        }
    }
});

参考:

在ASP.NET MVC中使用异步方法

类似的问题:

如何在mvc中使用语音

System.Speech.Synthesis在高CPU时挂起在2012 R2上

这篇关于如何在ASP.NET MVC中使用Windows语音合成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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