first commit

This commit is contained in:
cys841515238
2023-03-02 16:13:28 +08:00
commit 2733a60b97
741 changed files with 76931 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
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开放平台
private final static String appKey = "aKDun6vXyqPLhWMdoIsv87Ez";
// private final static String appKey = "eaPPX40oIazH8R4oWsD9U4IT";
// 填写申请百度语音申请的APP SECRET
private final static String secretKey = "ew9dMb4yGmwF1g4qutxNvogzjd9eP5tb";
// private final static String secretKey = "DTN0ioQywwM23IoT2ZzEBmvfBe63ATEY";
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
private final String text = "百度百科是百度公司推出的一部内容开放、自由的网络百科全书。";
// 发音人选择, 0为普通女声1为普通男生3为情感合成-度逍遥4为情感合成-度丫丫,默认为普通女声
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();
// 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;
}
// }
}
}