2011-06-13 94 views
2

我们有一个MVC网站,现在我们想添加一个功能,我们希望能够在客户端读出文本。我们显示一组发票,并且想要读出发票明细。我知道你可以在桌面应用程序中使用System.Speech库,通过传递细节来读取它,但有人知道我们如何在MVC项目中做到这一点。ASP.NEt MVC文本到语音

回答

2

好吧,这只是我的头的顶部,它尚未经过测试,但你也许可以做这样的事情:

public ActionResult Speak(string text) 
{ 
    var speech = new SpeechSynthesizer(); 
    speech.Speak(text); 

    byte[] bytes; 
    using (var stream = new MemoryStream()) 
    { 
     speech.SetOutputToWaveStream(stream); 
     bytes = stream.ToArray(); 
    } 
    return File(bytes, "audio/x-wav"); 
} 
0

我研究,我发现这种方式,其类似约翰Kalberer的回答是:

public async Task<ActionResult> Index() 
    { 
     Task<FileContentResult> task = Task.Run(() => 
     { 
      using (var synth = new SpeechSynthesizer()) 
      { 
       synth.SelectVoice("Microsoft Sabina Desktop"); 

       using (var stream = new MemoryStream()) 
       { 
        synth.SetOutputToWaveStream(stream); 
        synth.Speak("hola mundo"); 
        byte[] bytes = stream.GetBuffer(); 
        return File(bytes, "audio/x-wav"); 
       } 
      } 
     }); 

     return await task; 
    } 

在这种情况下,我使用的是西班牙语的声音,当然你可以选择你的声音或避免synth.SelectVoice并使用predeterminated