我正在使用此代码转换&使用保存文本到语音 * 使用System.Speech.Synthesis; * 但它抛出 - 异步操作只能在PreRenderComplete事件之前的页面上启动。 这是我的代码:
using (Stream ret = new MemoryStream())
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
var mi = synth.GetType().GetMethod("SetOutputStream", BindingFlags.Instance | BindingFlags.NonPublic);
var fmt = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Eight, AudioChannel.Mono);
mi.Invoke(synth, new object[] { ret, fmt, true, true });
synth.Speak("Greetings from stack overflow");
// Testing code:
using (var fs = new FileStream(@"c:\temp\test.wav", FileMode.Create, FileAccess.Write, FileShare.None))
{
ret.Position = 0;
byte[] buffer = new byte[4096];
for (; ; )
{
int len = ret.Read(buffer, 0, buffer.Length);
if (len == 0) break;
fs.Write(buffer, 0, len);
}
}
}
答案 0 :(得分:0)
将此添加到页面加载中。
protected void Page_Load(object sender, EventArgs e)
{
System.ComponentModel.AsyncOperationManager.SynchronizationContext =
new System.Threading.SynchronizationContext();
}