日期:2014-06-10 浏览次数:20658 次
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 引用 Speech sdk 5.1 在COM选项卡里面的Microsoft Speech object library引用 已经有11.0版本 using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; namespace Speech { /// <summary> /// Csharp: Google TTS API 文本语音读取 /// 涂聚文 /// </summary> public partial class Form2 : Form { /// <summary> /// /// </summary> /// <param name="FileName"></param> public void PlaySound(string FileName) { //要加载COM组件: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 = "中华人民共和国"; //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"; //could also be tts.wav string s = "中华人民共和国涂聚文投诉"; 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> /// 英文可以,中文不行 /// </summary> /// <param name="url"></param> public void PlayMp3FromUrl(string url) { try { url = HttpUtility.UrlDecode(url); using (Stream ms = new MemoryStream()) { using (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 button1_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(); } } }