您的位置:首页 > 其它

TTS文字转语音工具汇总

2014-05-02 17:43 826 查看
1.谷歌翻译的TTS

通过网络调用

http://translate.google.com/translate_tts?tl=zh&q=URL转码的文字

tl属性:语言种类,可以更改,如en,zh

q属性:文本,需要URL转码。

返回的是mpeglayer3?音频文件。

问题1:

页面使用js的window.open(url,name,param)打开新窗口,或者iframe跳转,发现url汉字部分总是乱码。

解决办法:

后来在页面添加encodePage=utf-8解决了。

问题2:

使用window.open打开的页面,不能自动播放google tts语音,必须手动在地址栏按下回车才有,url参数变化,又不能自动播放。

原因:

我所使用的是FireFox浏览器,会自动携带referer,google对http请求头的referer进行了屏蔽,可以参见此文:Google Text-To-Speech

......

Another restriction is that the service returns 404 Not Found if the request includes a Referer header (presumably one that is not for translate.google.com).

In spite of the limitations of the web service which certainly reflect the intention that the web service is only to be used by Google Translate, thanks to the new HTML5's Audio element and rel="noreferrer", the service may be utilized by client-side web applications like following (Google Chrome 4 recommended):
......

解决办法:TMD找不到阿...

2.讯飞语音API

网址:自己百度

申请比较容易,注册帐号然后新建相应平台的程序就可以得到appid,类似微博token一样用于验证的东东。

下载相关平台的SDK,将lib中的jar包导入eclipse项目,其中还有些批处理文件,.so是linux平台用的,.dll是windows平台用的,各自对应放好。

基本步骤可以参看SDK中的文档。

以下是一个简单的TTS程序:

import com.iflytek.speech.SpeechError;
import com.iflytek.speech.SynthesizerPlayer;
import com.iflytek.speech.SynthesizerPlayerListener;

public class GetTTS {
public static void main(String[] args){
private String appid="填写你得到的appid";  //填写你得到的appid
private String voiceName= "xiaoyan";     //发音人声
private int volume = 100;                //发音音量
private int voiceSpeed = 40;             //发音语速
private String backgroundSound = null;   //背景音乐,1是一首music
private String ttsBufferTime = "tts_buffer_time=2000";   //缓冲时间
private String sentence = "我是大笨蛋";            //你要说的话

//步骤一:获得播放器
SynthesizerPlayer tts = SynthesizerPlayer.createSynthesizerPlayer("appid=" + appid);

//步骤二:获得播放器监听器
/**
* SynthesizerPlayer Listener
*/
SynthesizerPlayerListener synListener = new SynthesizerPlayerListener() {
//缓冲
public void onBufferPercent(int percent, int beginPos, int endPos, String arg) {
System.out.println("正在缓冲:" + percent);
}
//播放结束
public void onEnd(SpeechError error) {
if (error == null)
System.out.println("播放完成");
else
System.out.println("错误代码:" + error.getErrorCode());
}
//开始播放
public void onPlayBegin() {
}
//播放暂停
public void onPlayPaused() {
}
//播放进度
public void onPlayPercent(int percent, int beginPos, int endPos) {
}
//恢复播放
public void onPlayResumed() {
}
};

//步骤三:设置参数
tts.setVoiceName(voiceName);
tts.setSpeed(voiceSpeed);
tts.setVolume(volume);
tts.setBackgroundSound(backgroundSound);

//步骤四:开始说话
tts.playText(sentence,ttsBufferTime,synListener);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: