使用百度智能云文字转语音服务还是蛮简单的,很容易上手
1.创建应用
首先要做的就是进入百度智能云后台界面,创建一个应用,复制API Key和Secret Key到我们的应用当中
百度智能云后台管理中心
2.编写文字转换语音工具类
import lombok.experimental.UtilityClass;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
/**
* @author Jonathan.WQ
* @date 2021年11月
*/
@UtilityClass
public class BaiduVoiceUtil {
// 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
private final int per = 0;
// 语速,取值0-9,默认为5中语速
private final int spd = 5;
// 音调,取值0-9,默认为5中语调
private final int pit = 5;
// 音量,取值0-9,默认为5中音量
private final int vol = 5;
// 可以使用https
public final String url = "http://tsn.baidu.com/text2audio";
// 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
private String cuid = "1234567JAVA";
/**
* @param appKey 百度语音申请的appkey
* @param secretKey 百度语音申请的APP SECRET
* @param voiceText 待转换的文本内容
* @param savePath 语音文件保存的地址
* @throws Exception
*/
public String run(String appKey, String secretKey, String voiceText, String savePath) throws Exception {
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
holder.resfresh();
String token = holder.getToken();
String url2 = url + "?tex=" + ConnUtil.urlEncode(voiceText);
url2 += "&per=" + per;
url2 += "&spd=" + spd;
url2 += "&pit=" + pit;
url2 += "&vol=" + vol;
String macAddress = getMacAddress();
if (StringUtils.isNotNull(macAddress)) {
url2 += "&cuid=" + macAddress;
} else {
url2 += "&cuid=" + cuid;
}
url2 += "&tok=" + token;
url2 += "&lan=zh&ctp=1";
// 反馈请带上此url,浏览器上可以测试
// System.out.println(url2);
HttpURLConnection conn = (HttpURLConnection) new URL(url2).openConnection();
conn.setConnectTimeout(5000);
String contentType = conn.getContentType();
if (contentType.contains("mp3")) {
byte[] bytes = ConnUtil.getResponseBytes(conn);
// 打开mp3文件即可播放
String savePathUrl = savePath + IdUtils.getInstanse().getUID() + ".mp3";
File audioFile = new File(savePathUrl);
if (!audioFile.exists()) {
audioFile.getParentFile().mkdirs();
audioFile.createNewFile();
}
// System.out.println( file.getAbsolutePath());
FileOutputStream os = new FileOutputStream(audioFile);
os.write(bytes);
os.close();
System.out.println("mp3 file write to " + audioFile.getAbsolutePath());
return savePathUrl;
} else {
System.err.println("ERROR: content-type= " + contentType);
String res = ConnUtil.getResponseString(conn);
System.err.println(res);
return null;
}
}
// 获取mac地址
public String getMacAddress() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
byte[] mac = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
continue;
} else {
mac = netInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
if (sb.length() > 0) {
return sb.toString();
}
}
}
}
} catch (Exception e) {
log.error("MAC地址获取失败", e);
}
return "";
}
}