csharp:Google TTS API text to speech

2020-11-16 00:20

阅读:500

标签:com   class   blog   http   code   div   string   tar   ext   art   tab   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
{
    ///
    /// Csharp: Google TTS API 文本语音读取
    ///  涂聚文
    ///
    public partial class Form2 : Form
    {
        ///
        ///
        ///
        ///
        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();
        }
        ///
        ///
        ///
        public Form2()
        {
            InitializeComponent();
        }
        ///
        ///
        ///
        ///
        ///
        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);
        ///
        /// 英文可以,中文不行
        ///
        ///
        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();
            }
        }
        ///
        /// 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
        ///
        ///
        ///
        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();
        }
    }
}

 

csharp:Google TTS API text to speech,搜素材,soscw.com

csharp:Google TTS API text to speech

标签:com   class   blog   http   code   div   string   tar   ext   art   tab   

原文地址:http://www.cnblogs.com/geovindu/p/3695232.html


评论


亲,登录后才可以留言!