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

csharp:Google TTS API text to speech的更多相关文章

  1. 第二篇:TTS(Text To Speech)语音合成

    TTS(Text To Speech)语音合成: 百度AI语音合成SDK文档--python SDK: https://ai.baidu.com/docs#/TTS-Online-Python-SDK ...

  2. xe5 android tts(Text To Speech)

    xe5 android  tts(Text To Speech) TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转 ...

  3. delphi xe5 android tts(Text To Speech)

    TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转语音的方法 和访问蓝牙一样,这里用javaclass的接口实现 接 ...

  4. xe5 android tts(Text To Speech) [转]

    TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转语音的方法 和访问蓝牙一样,这里用javaclass的接口实现 接 ...

  5. 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML

    [源码下载] 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML 作者:webabcd 介绍重新想象 Windows 8.1 ...

  6. 必应语音API(Bing text to speech API)

    前言 Link : Microsoft Speech API overview 通过这个链接,大致了解Bing speech API的语音识别和语音合成两部分, 这次是需要用到TTS,所以就直接看TT ...

  7. 通过google cloud API 使用 WaveNet

    Cloud Text-to-Speech 中使用了WaveNet,用于TTS,页面上有Demo.目前是BETA版 使用方法 注册及认证参考:Quickstart: Text-to-Speech 安装g ...

  8. Google Map API V3开发(2)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  9. Google Map API V3开发(3)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

随机推荐

  1. python爬虫urllib库使用

    urllib包括以下四个模块: 1.request:基本的HTTP请求模块,可以用来模拟发送请求.就像在浏览器里输入网址然后回车一样,只需要给库方法传入URL以及额外的参数,就可以模拟实现这个过程. ...

  2. 启动MacOS 本地服务

    MacOS 自带Apatch服务器, 在浏览器输入 http://127.0.0.1/  出现it works,代表访问成功 一. 启动 启动 sudo apachectl start 重启 sudo ...

  3. tomcat增加运行内存

    内容为: set JAVA_OPTS=%JAVA_OPTS% -server -Xms2048m -Xmx2048m -XX:PermSize=212M -XX:MaxPermSize=512m 在m ...

  4. linux ssh 上传 文件

    在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地 ...

  5. php 多字节编码转换

    PHP 支持的编码 mb_convert_encoding — 转换字符的编码 string mb_convert_encoding ( string $str , string $to_encodi ...

  6. POJ_2155 Matrix 【二维树状数组】

    一.题面 POJ2155 二.分析 楼教主出的题,是二维树状数组非常好的题,还结合了开关问题(开关变化的次数如果为偶数,状态不变,奇数状态相反). 题意就是给了一个二维的坐标平面,每个点初始值都是0, ...

  7. 2018牛客多校1 - J Different Integers 莫队/主席树签到

    题意:给出n<5e4,a[1...n],单次1e5总次1e6次查询除去区间(L,R)的数的个数 开场5分钟:莫队是不可能莫队的,这道题是不可能莫队的 最后1小时:真香 具体操作没啥特别的,注意一 ...

  8. jenkins 重置构建历史

    item = Jenkins.instance.getItemByFullName("98")//THIS WILL REMOVE ALL BUILD HISTORYitem.bu ...

  9. mkvirtualenv创建python虚拟环境

    为什么要创建虚拟环境呢? 如果在我们本地开发好几个项目,每个项目又用到很多第三方插件,如果没没用创建虚拟环境,那么本地项目插件就会很多很乱,如果有虚拟环境就不一样了 虚拟环境安装 pip instal ...

  10. python 中mysql数据库的读写

    1.读取数据库 import pymysql id=[] name=[] explain=[] db=pymysql.Connection(host=,user="root", p ...