ASP. NET implementation TTS (semi-finished)

Source: Internet
Author: User

Before using WinForm to achieve TTS has been successful, the next system is called the class library. But when I moved the same code to ASP, I encountered a lot of problems, looked up a lot of websites, tried a lot of methods, and now I have made a part of it.

Before calling Microsoft TTS is using this code

 1  speechsynthesizer speech = new  Span style= "color: #000000;" > SpeechSynthesizer ();  2  public  void  Speak (string  text, int   rate)  3  { 4  speech. Rate = rate;  5   speech. Speak (text);  6   speech. Dispose ();  7 } 

On the Web page, I assign this method to the button's Click event. The first time there is a sound, but after the sound playback after the page is still loaded, and then press button also did not respond. After debugging, also found that the event is over. Feel very messy, and then want to use Ajax to let it execute and generate the audio file, the first click on the button generated very smoothly, but click on the second time there is a problem, it has been long before the generation, like stuck, a small partner also encountered the same problem. First, the AJAX code:

1<script type="Text/javascript">2 $ (document). Ready (function () {3$("#bt_read"). Click (function () {4                 varurl = window.location.href +"/textspeak";5 $.ajax ({6Type"POST",7 Url:url,8Data"{' text ': ' Hello '}",9ContentType:"Application/json;charset=utf-8",TenDataType:"JSON", One error:function () { AAlert"Error"); -                     } -                 }); the             }); -         }) -</script>

This is the code behind the scenes:

1 [WebMethod]2      Public Static voidTextspeak (stringtext)3     {4Texttospeak Ttspeak =Newtexttospeak ();5         stringx = DateTime.Now.ToString ("YYYYMMDDHHMMSS");6Ttspeak.savewavefile ("d:/"+ x +". wav", text,1,"male");7}

(The Code for class Texttospeak in the previous article)

I hope the great God can see to help me solve this problem (if the big God does not see, you can only slowly tangled >_<)-----If you want to see the solution of the small partner, you can ignore the above code:

Try a lot of methods, just this can be done (also add a DotNetSpeech.dll class library):

    /// <summary>    ///use TTS on Web pages/// </summary>    /// <param name= "path" >Path</param>    /// <param name= "text" >text</param>    /// <param name= "rate" >Speed</param>    /// <param name= "Gender" >Sex</param>     Public voidSavewavefile (stringPathstringTextintRatestringgender) {Type type= Type.gettypefromprogid ("SAPI. SpVoice"); Dynamic SpVoice= Activator.CreateInstance (type);//This is mostly used to call Dotnetspeech.        varobj =spvoice.getvoices (); if(gender. ToLower () = ="male")        {             for(inti =0; I < obj. Count; i++)            {                stringdesc =obj. Item (i).                GetDescription (); if(Desc. Contains ("Paul") ) {Spvoice.voice=obj.                Item (i); }            }        }        Else if(gender. ToLower () = ="female")        {              for(inti =0; I < obj. Count; i++)            {                stringdesc =obj. Item (i).                GetDescription (); if(Desc. Contains ("Kate") ) {Spvoice.voice=obj.                Item (i); } }} spvoice.rate=Rate ; Speechstreamfilemode Spffilemode=Speechstreamfilemode.ssfmcreateforwrite; Spfilestream Spfilestream=NewSpfilestreamclass (); Spfilestream.open (Path, Spffilemode,false); Spvoice.audiooutputstream=Spfilestream;        Spvoice.speak (text); Spvoice.waituntildone ( +);    Spfilestream.close (); }

Why didn't I write the Speak method? To play audio on the Web client, make the server speak. The server shouted dumb, the client is also inaudible OH ~ ~ can only generate audio files uploaded to the Web page with plug-in playback. Why I say is semi-finished, because I tried a lot of methods, always find the right plugin to solve the problem of playback.

It was suddenly discovered that it could be generated directly. MP3, the internet said to generate WAV format, I did not try, until the small partners to see the path written directly *. MP3, and can be generated only to know that the original can be directly output MP3 wow. So the problem is not big, find a plugin to the path attached to the line ~ ~

Plug-ins to meet these two conditions:

1. Ability to add links dynamically.

2. Can play. wav format.

I tried the MediaPlayer plugin written with a custom control to play the reference: http://www.cnblogs.com/insus/archive/2009/05/03/1448097.html

In fact, this can solve the problem. It is only in IE has the effect, the other browser does not work. Read the page code, audio links are read, but just can't play ~ ~

There is also a result method that allows the SWF player to play a TTS generated WAV file, or convert WAV to MP3. I haven't been able to make it yet.

First here, I am still slowly debugging, and so out, and immediately sent out. I also hope that the great God to help me to solve the next Wow ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.