Csharp: Google tts api text to speech

Source: Internet
Author: User

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO; using System. net; using System. threading; using NAudio. wave ;// http://naudio.codeplex.com/using NAudio. coreAudioApi; using System. web; using System. media; using SpeechLib; // NET2.0 references the Microsoft Speech object library Reference of Speech sdk 5.1 on the COM tab. Version 11.0 of using Microsoft. directX; using Microsoft. directX. directSound; namespace Speech {// <summary> // Csharp: Google tts api text Speech read // tu juwen // </summary> public partial class Form2: form {/// <summary> ///// </summary> /// <param name = "FileName"> </pa Ram> public void PlaySound (string FileName) {// to load the COM component: Microsoft speech object Library if (! System. IO. file. exists (FileName) {return;} SpeechLib. spVoiceClass pp = new SpeechLib. spVoiceClass (); SpeechLib. spFileStreamClass spFs = new SpeechLib. spFileStreamClass (); spFs. open (FileName, SpeechLib. speechStreamFileMode. SSFMOpenForRead, true); SpeechLib. ISpeechBaseStream Istream = spFs as SpeechLib. ISpeechBaseStream; pp. speakStream (Istream, SpeechLib. speechVoiceSpeakFlags. SVSFIsFilename); spFs. close () ;}/// <summary> ///// </summary> public Form2 () {InitializeComponent ();} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void Form2_Load (object sender, eventArgs e) {this. textBox1.Text = "People's Republic of China"; // OK WebClient web = new WebClient (); web. headers. add (HttpRequestHeader. userAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)"); string encstr = string. empty; string filename = "tts.mp3"; // cocould also be tts.wav string s = "People's Republic of China tu juwen complaint"; encstr = Uri. escapeDataString (s); Console. writeLine (encstr); web. downloadFile (" http://translate.google.com/translate_tts?ie=UTF-8&tl=zh-cn&q= "+ Encstr ,". \ "+ filename); // PlaySound (Application. startupPath + "\" + filename); // SoundPlayer sp = new SoundPlayer (); // sp. soundLocation = Application. startupPath + "\" + filename; // sp. loadAsync (); // sp. playSync (); // sp. playLooping (); // Device dv = new Device (); // SecondaryBuffer buf = new SecondaryBuffer (Application. startupPath + "\" + filename, dv); // buf. play (0, BufferPlayFlags. looping); this. axWindowsMediaPlayer1.URL = Application. startupPath + "\" + filename;} bool waiting = false; AutoResetEvent stop = new AutoResetEvent (false); // <summary> // The name can be in English, chinese characters are unavailable /// </summary> /// <param name = "url"> </param> public void PlayMp3FromUrl (string url) {try {url = HttpUtility. urlDecode (url); using (Stream MS = new MemoryStream () {using (Stream stream Stream = WebRequest. create (url ). getResponse (). getResponseStream () // HttpUtility. urlDecode ({// UTF8Encoding encoding = new UTF8Encoding (); // byte [] buffer = encoding. getBytes (url); // stream. write (buffer, 0, buffer. length); // stream. close (); byte [] buffer = new byte [32768]; // 32768 int read; while (read = stream. read (buffer, 0, buffer. length)> 0) {ms. write (buffer, 0, read);} // using (StreamWriter writer = new StreamWriter (stream, Encoding. UTF8) // {// writer. write (url); //} // using (Stream stream = request. getRequestStream () // using (StreamWriter writer = new StreamWriter (requestStream, Encoding. UTF8) // {// writer. write (url); //} ms. position = 0; using (WaveStream blockAlignedStream = new BlockAlignReductionStream (WaveFormatConversionStream. createPcmStream (new Mp3FileReader (MS) {using (WaveOut waveOut = new WaveOut (WaveCallbackInfo. functionCallback () {waveOut. init (blockAlignedStream); waveOut. playbackStopped + = (sender, e) => {waveOut. stop () ;}; waveOut. play (); waiting = true; stop. waitOne (10000); waiting = false ;}}} catch (Exception ex) {ex. message. toString () ;}/// <summary> /// http://translate.google.com/translate_tts?tl=zh-cn&q=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD /// http://translate.google.cn/translate_tts?ie=UTF-8&q=%E4%B8%AD%E5%9B%BD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD&tl=zh-cn&prev=input /// </Summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button#click (object sender, eventArgs e) {// var playThread = new Thread () => PlayMp3FromUrl (" http://translate.google.com/translate_tts?tl=en&q= "+ HttpUtility. UrlEncode (this. textBox1.Text); string str = HttpUtility. UrlEncode (this. textBox1.Text); var playThread = new Thread () => PlayMp3FromUrl (" http://translate.google.com/translate_tts?ie=UTF-8&tl=zh-cn&q= "+ Str); // HttpUtility. UrlEncode (playThread. IsBackground = true; playThread. Start ();}}}

 

Related Article

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.