使用SpeechLib托管问题

本文关键字:问题 SpeechLib 使用 | 更新日期: 2023-09-27 18:28:29

嗨,我正在使用c#和VS2010开发一个web项目。我正在使用SpeechLib,它可以将文本转换为语音。在我的电脑中,本地的一切都很好,但当托管网页时,页面不起作用,并输出错误500。

正如另一篇旧文章(链接)所说,问题似乎是这个库试图在没有必要权限的情况下在文件夹中写入临时文件。问题没有解决。

我该如何解决这个问题?谢谢

使用SpeechLib托管问题

解决磁盘权限问题的一种方法是不将音频保存到磁盘。为此,您需要将输出转换为流对象,该流对象可以通过网页、HTTP处理程序或MVC操作返回。遗憾的是,"SetOutputToAudioStream"只返回原始PCM音频。

为了输出其他编码,如µ-law(mu-law、u-law、ulaw),您必须通过使用反射来访问非公共的SetOutputStream方法。下面是一个实现这一点并返回字节数组的代码片段:

using System.Reflection;
/* Beginning Code */
byte outputWavBytes;
MemoryStream outputWav = new MemoryStream()
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    var mi = synth.GetType().GetMethod("SetOutputStream", BindingFlags.Instance | BindingFlags.NonPublic);
    var fmt = new SpeechAudioFormatInfo(EncodingFormat.ULaw, 8000, 8, 1, 20, 2, null)
    mi.Invoke(synth, new object[] { outputWav, fmt, true, true });
    synth.Speak("This is a test to stream a different encoding.");
    outputWav.Seek(0, SeekOrigin.Begin);
    outputWavBytes = outputWav.GetBuffer();
}

/* End Code */