本帖子主要示例经过REST API进行语音合成。使用Java语言进行示例Demo测试css
经过GET方式获取access_token并保存备用java
24.259f1f35b9f628b5910d4264593aeffe.2592000.1515113352.282335-123456
看过RESTAPI文档会得知请求方式为GET 返回的内容是音频文件的流对象 而且默认返回是MP3格式 web
Content-type--->[audio/mp3]api
那咱们就写一个HTTP获得数据并保存为MP3的方法app
/** * 语音合成HTTP方法 * @param requestUrl 请求的接口地址 拼接access_token后的 * @param params 语音合成的参数 * @throws Exception */
public static String postVoice(String requestUrl,String params) throws Exception {
String workspace = System.getProperty("user.home");
String path = workspace+"/text2audio/";
try {
if (!(new File(path).isDirectory())) {
new File(path).mkdir();
}
} catch (SecurityException e) {
e.printStackTrace();
}
String filePath = path+"VOICE"+new Date().getTime()/1000+".mp3";
String generalUrl = requestUrl;
URL url = new URL(generalUrl);
System.out.println(generalUrl);
// 打开和URL之间的链接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("打开连接,开始发送请求"+new Date().getTime()/1000);
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 获得请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
// 创建实际的链接
connection.connect();
// 获取全部响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历全部的响应头字段
for (String key : headers.keySet()) {
System.out.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int len = -1;
while ((len=inputStream.read(buffer))!=-1) {
outputStream.write(buffer,0,len);
}
outputStream.close();
System.out.println("请求结束"+new Date().getTime()/1000);
System.out.println("MP3文件保存目录:" + filePath);
return filePath;
}
请求参数须要一个cuid用户惟一标识,用来区分用户,计算UV值。长度为60字符之内 那就再写一个简单的随机数方法dom
import java.util.Random;
/** * 获取指定长度的随机字符串 * @author 小帅丶 * @Title RandomStringGenerator * @时间 2017-5-26下午4:58:53 */
public class RandomStringGenerator {
/** * 获取必定长度的随机字符串 * @param length 指定字符串长度 * @return 必定长度的字符串 */
public static String getRandomStringByLength(int length) {
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}
--------------------------接下来进行文本合成语音的示例代码--------------------------post
package com.xs.audio.tns;
import java.net.URLEncoder;
import com.xs.common.APIContants;
import com.xs.util.baidu.HttpUtil;
import com.xs.util.baidu.RandomStringGenerator;
/** * 百度语音合成JavaDemo(非官方) * @author 小帅丶 * @date 2017-5-26上午11:17:32 */
public class Text2Audio {
public String TEXT2AUDIO_URL = "http://tsn.baidu.com/text2audio";
public static void main(String[] args) throws Exception {
String tex = "开发者小帅你好";
Text2Audio audio = new Text2Audio();
audio.text2Audio(tex, "开始要求保存的AccessToken", "1", RandomStringGenerator.getRandomStringByLength(60));
}
/** * 全部参数方法 * @Title text2Audio * @param tex 必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节 * @param lan 必填 语言选择,填写zh * @param tok 必填 开放平台获取到的开发者access_token * @param ctp 必填 客户端类型选择,web端填写1 * @param cuid 必填 用户惟一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60之内 * @param spd 选填 语速,取值0-9,默认为5中语速 * @param pit 选填 音调,取值0-9,默认为5中语调 * @param vol 选填 音量,取值0-9,默认为5中音量 * @param per 选填 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声 * @author 小帅丶 * @throws Exception * @date 2017-5-26 */
@SuppressWarnings("static-access")
public void text2Audio(String tex,String tok,String ctp,String cuid,String spd,String pit,String vol,String per) throws Exception{
String params = "tex=" + URLEncoder.encode(tex, "UTF-8")
+ "&lan=zh&cuid=" + cuid + "&ctp=1&tok=" + tok + "&spd=" + spd
+ "&pit=" + pit + "&vol=" + vol + "&per=" + per;
System.out.println(params);
HttpUtil httpUtil = new HttpUtil();
String data = httpUtil.postVoice(TEXT2AUDIO_URL,params);
System.out.println("文件保存路径:"+data);
}
/** * 必填参数方法 * @Title text2Audio * @param tex 必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节 * @param lan 必填 语言选择,填写zh * @param tok 必填 开放平台获取到的开发者access_token * @param ctp 必填 客户端类型选择,web端填写1 * @param cuid 必填 用户惟一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60之内 * @author 小帅丶 * @throws Exception * @date 2017-5-26 */
@SuppressWarnings("static-access")
public void text2Audio(String tex,String tok,String ctp,String cuid) throws Exception{
String params = "tex=" + URLEncoder.encode(tex, "UTF-8")
+ "&lan=zh&cuid=" + cuid + "&ctp=1&tok=" + tok;
System.out.println(params);
HttpUtil httpUtil = new HttpUtil();
String data = httpUtil.postVoice(TEXT2AUDIO_URL,params);
System.out.println("文件保存路径:"+data);
}
}
参数无误会返回MP3的存放路径测试
MP3文件保存目录:C:\Users\Administrator/text2audio/VOICE1512521962.mp3
文件保存路径:C:\Users\Administrator/text2audio/VOICE1512521962.mp3
以上就是JavaAPI方式语音合成示例ui
下一贴会发布Java方法实现MP3转PCM 并进行语音识别示例代码编码