赞
踩
【实例简介】
【实例截图】
【核心代码】
package com.baidu.speech.restapi.ttsdemo;
import com.baidu.speech.restapi.common.DemoException;
import com.baidu.speech.restapi.common.ConnUtil;
import com.baidu.speech.restapi.common.TokenHolder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class TtsMain {
public static void main(String[] args) throws IOException, DemoException {
(new TtsMain()).run();
}
// 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx"
private final String appKey = "4E1BG9lTnlSeIf1NQFlrSq6h";
// 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx"
private final String secretKey = "544ca4657ba8002e3dea3ac2f5fdd241";
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
// 可以百度搜索"urlencode"
private final String text = "欢迎使用百度语音";
// 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
private final int per = 0;
// 语速,取值0-15,默认为5中语速
private final int spd = 5;
// 音调,取值0-15,默认为5中语调
private final int pit = 5;
// 音量,取值0-9,默认为5中音量
private final int vol = 5;
// 下载的文件格式, 3:mp3(default) 4: pcm-16k 5: pcm-8k 6. wav
private final int aue = 6;
public final String url = "http://tsn.baidu.com/text2audio"; // 可以使用https
private String cuid = "1234567JAVA";
private void run() throws IOException, DemoException {
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
holder.refresh();
String token = holder.getToken();
// 此处2次urlencode, 确保特殊字符被正确编码
String params = "tex=" ConnUtil.urlEncode(ConnUtil.urlEncode(text));
params = "&per=" per;
params = "&spd=" spd;
params = "&pit=" pit;
params = "&vol=" vol;
params = "&cuid=" cuid;
params = "&tok=" token;
params = "&aue=" aue;
params = "&lan=zh&ctp=1";
System.out.println(url "?" params); // 反馈请带上此url,浏览器上可以测试
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(5000);
PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
printWriter.write(params);
printWriter.close();
String contentType = conn.getContentType();
if (contentType.contains("audio/")) {
byte[] bytes = ConnUtil.getResponseBytes(conn);
String format = getFormat(aue);
File file = new File("result." format); // 打开mp3文件即可播放
// System.out.println( file.getAbsolutePath());
FileOutputStream os = new FileOutputStream(file);
os.write(bytes);
os.close();
System.out.println("audio file write to " file.getAbsolutePath());
} else {
System.err.println("ERROR: content-type= " contentType);
String res = ConnUtil.getResponseString(conn);
System.err.println(res);
}
}
// 下载的文件格式, 3:mp3(default) 4: pcm-16k 5: pcm-8k 6. wav
private String getFormat(int aue) {
String[] formats = {"mp3", "pcm", "pcm", "wav"};
return formats[aue - 3];
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。