84 lines
3.6 KiB
Java
84 lines
3.6 KiB
Java
package com.peanut.common.utils;
|
||
|
||
|
||
import java.io.File;
|
||
import java.io.FileOutputStream;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.util.UUID;
|
||
|
||
public class BaiduVoicesUtils {
|
||
|
||
// 填写申请百度语音申请的appkey 申请地址:百度AI开放平台
|
||
//todo private final static String appKey = "aKDun6vXyqPLhWMdoIsv87Ez";
|
||
|
||
// private final static String appKey = "eaPPX40oIazH8R4oWsD9U4IT";
|
||
private final static String appKey = "xkyZMti9hu6KSQ8PdRDsadqI";
|
||
|
||
// 填写申请百度语音申请的APP SECRET
|
||
// todo private final static String secretKey = "ew9dMb4yGmwF1g4qutxNvogzjd9eP5tb";
|
||
|
||
// private final static String secretKey = "DTN0ioQywwM23IoT2ZzEBmvfBe63ATEY";
|
||
private final static String secretKey = "KQvdrrADpV7hBzptqvakwG56dYuGljeg";
|
||
|
||
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
|
||
private final String text = "百度百科是百度公司推出的一部内容开放、自由的网络百科全书。";
|
||
// 基础 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
|
||
//度逍遥(精品)=5003,度小鹿=5118,度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5
|
||
// private final static int per = 3;
|
||
private final static int per =3;
|
||
// 语速,取值0-9,默认为5中语速
|
||
private final static int spd = 5;
|
||
// 音调,取值0-9,默认为5中语调
|
||
private final static int pit = 5;
|
||
// 音量,取值0-9,默认为5中音量
|
||
private final static int vol = 5;
|
||
// 调用地址
|
||
public final static String url = "http://tsn.baidu.com/text2audio";
|
||
// 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
|
||
private static String cuid = "0322java";
|
||
|
||
public static String run(String content) throws Exception {
|
||
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
|
||
holder.resfresh();
|
||
String token = holder.getToken();
|
||
String token1 = holder.getToken();
|
||
final String s = ConnUtil.urlEncode(content);
|
||
String url21 = url +"?tex=" + s;
|
||
url21 +="&per="+ per;
|
||
url21 +=""+cuid;
|
||
url21 +=""+vol;
|
||
long expiresAt = holder.getExpiresAt();
|
||
// for (String a:list) {
|
||
String url2 = url + "?tex=" + ConnUtil.urlEncode(content);
|
||
url2 += "&per=" + per;
|
||
url2 += "&spd=" + spd;
|
||
url2 += "&pit=" + pit;
|
||
url2 += "&vol=" + vol;
|
||
url2 += "&cuid=" + cuid;
|
||
url2 += "&tok=" + token;
|
||
url2 += "&lan=zh&ctp=1";
|
||
HttpURLConnection conn = (HttpURLConnection) new URL(url2).openConnection();
|
||
conn.setConnectTimeout(5000);
|
||
String contentType = conn.getContentType();
|
||
if (contentType.contains("mp3")) {
|
||
byte[] bytes = ConnUtil.getResponseBytes(conn);
|
||
// 存在项目根目录下,去文件夹点击可以播放
|
||
String substring = UUID.randomUUID().toString().substring(0, 6);
|
||
File file = new File(substring+".mp3");
|
||
FileOutputStream os = new FileOutputStream(file);
|
||
os.write(bytes);
|
||
os.close();
|
||
System.out.println("mp3 file write to " + file.getAbsolutePath());
|
||
return file.getAbsolutePath();
|
||
} else {
|
||
System.err.println("ERROR: content-type= " + contentType);
|
||
String res = ConnUtil.getResponseString(conn);
|
||
System.err.println(res);
|
||
return res;
|
||
}
|
||
// }
|
||
|
||
}
|
||
}
|