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. dotnet --info

    [root@bogon ~]# dotnet --info.NET Command Line Tools (2.1.4) Product Information: Version: 2.1.4 Com ...

  2. 2016级算法第一次练习赛-D.AlvinZH的儿时回忆——跳房子

    864 AlvinZH的儿时回忆----跳房子 题目链接:https://buaacoding.cn/problem/864/index 思路 这是一道简单题,但是同样有人想复杂了,DP?大模拟?. ...

  3. tomcat Setting property 'source' to 'org.eclipse.jst.jee.server

    很不爽,好好的项目不知道突然怎么了... 启动tomcat出个错,,,,, setting property 'source' to 'org.eclipse.jst.jee.server:jeesi ...

  4. Q678 有效的括号字符串

    给定一个只包含三种字符的字符串:(,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须有相应的左括号 ( ...

  5. 启用mysql日志记录执行过的sql

    在mysql命令行或者客户端管理工具中执行:SHOW VARIABLES LIKE "general_log%"; 结果: general_log OFF general_log_ ...

  6. 利用 Nginx 做反向代理解决微信小程序业务域名限制问题

    参考网站:(下述两个参考网站可以完美解决问题) https://www.jianshu.com/p/47eff6eeec25 https://www.cnblogs.com/kenwar/p/8288 ...

  7. SpringCloud与Docker微服务架构实战笔记

    一  微服务架构概述 1. 单体应用架构存在的问题 结合:https://www.cnblogs.com/jialanshun/p/10637454.html一起看,在该篇博客中搜索“单块架构的优缺点 ...

  8. js Object.keys 用法

    Object.keys()   可以将对象里的所以的键取出来放到一个数组中 1.传入对象, 返回 包含对象可枚举属性和方法的数组 var obj = {'a': 'Beijing', 'b': 'Ha ...

  9. es第一篇:Getting Started

    es是一个近乎实时的搜索平台,这意味着从索引文档到文档可搜索,是有一点点延迟的(通常是一秒).es集群是一个或多个节点的集合,它们共同保存数据,并提供跨所有节点的联合索引和搜索功能.集群名由clust ...

  10. bitset用法

    学习博客:https://www.cnblogs.com/magisk/p/8809922.html C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0 ...