转换&在asp.net中将文本保存到语音文件c# [英] convert & save text to speech file in asp.net c#

查看:85
本文介绍了转换&在asp.net中将文本保存到语音文件c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码转换&使用System.Speech.Synthesis使用

保存文本到语音;

但它会抛出 -

异步操作可以只能在PreRenderComplete活动之前的页面上启动。

这是我的代码请帮助我...



I am using this code to convert & save text to speech using
using System.Speech.Synthesis;
but it throws -
asynchronous operation can only be started on a page prior to PreRenderComplete event.
this is my code please help me...

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

推荐答案

我想你有这个代码Page_Load事件,在呈现页面之前执行。看看这个:

http://msdn.microsoft.com/en- us / library / ms178472.ASPX

您会发现可以在PreRenderComplete事件处理程序中执行此代码
I suppose you have this code in the Page_Load event, which executes before rendering the page. Look at this:
http://msdn.microsoft.com/en-us/library/ms178472.ASPX
and you will find you can execute this code in the PreRenderComplete eventhandler


这篇关于转换&在asp.net中将文本保存到语音文件c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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