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. js中的类和对象以及自定义对象

    js中的类 1.类的声明 function Person(name,age){ this.name=name; this.age=age; this.test=function(a){ alert(a ...

  2. C#-★结构体★

    结构体: 结构体类型是用户自己定义的一种类型,它是由其他类型组合而成的,可包含构造函数.常数.字段.方法.属性.索引器.运算符.事件和嵌套类型的值类型. 结构体在几个方面不同于类:结构体为值类型而不是 ...

  3. IDEA通过Maven WebApp archetype 创建Spring boot项目骨架

    springboot项目资源: GitHub地址:https://github.com/TisFreedom/springbootdome.git 码云地址:https://gitee.com/Tis ...

  4. mongoDB使用小记

    1.简介: MongoDB是由c++语言编写的,基于分布式文件存储的开源数据库系统.MongoDB将数据存储为一个文档,数据结构有键-值对,类似于JSON对象. MongoDB其中的一些概念如下: M ...

  5. oracle client 低于 oracle server 端,导致报错ORA-01882

    https://forums.toadworld.com/t/ora-01882-when-i-want-to-view-records-con-dba-scheduler-jobs-toad-10- ...

  6. DB link的迁移

    我们在做某些Schema的迁移的时候,由于用到Public的db link,然而由于不知道db link中目标端账号的密码,因此无法在新环境重新创建DB link. 本次实验的思路是将视图dba_db ...

  7. RabbitMQ的TopicExchange通配符问题

    TopicExchange交换机支持使用通配符*.# *号只能向后多匹配一层路径. #号可以向后匹配多层路径.

  8. Cloudera Manager安装之时间服务器和时间客户端(Ubuntu14.04)(二)

    第二步: Cloudera Manager安装之时间服务器和时间客户端(二) 找一台机器作为时间服务器 我这里,放到ubuntucmbigdata1这台机器! 注意,之前是已经做了集群时间同步了. 在 ...

  9. ruby中的retry和redo

    # retry用于处理异常中的begin/end代码块中,如果发生异常会重新运行 begin 和 rescue 之间的代码#当retry 被调用的话,begin 和 rescue 之间所有的代码都会被 ...

  10. OOP_由C到C++

    由C到C++ OOP第一课 C语言的局限 C++的特点 C++的程序特征 C++程序的结构特性 C++程序的编辑.编译和运行 ⭐C++对C的补充 C语言的局限 类型检查机制相对较弱,使得程序中的一些错 ...