Files
nuttyreading-java/src/main/java/com/peanut/common/utils/BaiduVoicesUtils.java
yc13649764453 0b193caa03 -- 新版提交
2023-09-09 13:51:35 +08:00

84 lines
3.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
// }
}
}