bug修改
This commit is contained in:
@ -56,4 +56,6 @@ public class AppBusinessFileBo extends BaseEntity {
|
||||
* 是否使用语音播报(0-否,1-是)
|
||||
*/
|
||||
private Integer useStatus;
|
||||
|
||||
private Long createBy;
|
||||
}
|
||||
|
||||
@ -113,14 +113,14 @@ public class AlibabaTTSUtil {
|
||||
DEFAULT_VOLUME, DEFAULT_SPEECH_RATE, DEFAULT_PITCH_RATE);
|
||||
}
|
||||
|
||||
public byte[] synthesizeTextToMp3(String text){
|
||||
public byte[] synthesizeTextToMp3(String text,String fileSuffix){
|
||||
try {
|
||||
// 获取访问令牌
|
||||
String token = getValidAccessToken();
|
||||
// 使用HTTP方式调用
|
||||
HttpTtsClient httpClient = new HttpTtsClient(appkey,token);
|
||||
|
||||
return httpClient.synthesizeTextToMp3(text);
|
||||
return httpClient.synthesizeTextToMp3(text,fileSuffix);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -9,7 +9,9 @@ import java.net.URL;
|
||||
import static cn.dev33.satoken.SaManager.log;
|
||||
|
||||
public class HttpTtsClient {
|
||||
|
||||
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String appKey;
|
||||
private String token;
|
||||
/**
|
||||
@ -17,36 +19,57 @@ public class HttpTtsClient {
|
||||
*/
|
||||
private static final String BASE_URL = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||
|
||||
public HttpTtsClient(String appKey, String token) {
|
||||
this.appKey = appKey;
|
||||
public HttpTtsClient(String accessKeyId, String token) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
this.token = token;
|
||||
// this.accessKeySecret = accessKeySecret;
|
||||
// this.appKey = appKey;
|
||||
}
|
||||
// private String refreshAccessToken() {
|
||||
// try {
|
||||
// // 调用阿里云API获取访问令牌
|
||||
// AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
||||
// accessToken.apply();
|
||||
// String token = accessToken.getToken();
|
||||
// log.info("访问令牌刷新成功");
|
||||
//
|
||||
// return token;
|
||||
// } catch (Exception e) {
|
||||
// log.error("刷新访问令牌失败: {}", e.getMessage(), e);
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* 使用HTTP POST方式调用阿里云TTS服务生成MP3格式语音
|
||||
*/
|
||||
public byte[] synthesizeTextToMp3(String text) throws IOException {
|
||||
String endpoint = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||
public byte[] synthesizeTextToMp3(String text,String fileSuffix) throws IOException {
|
||||
// String endpoint = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||
|
||||
// 构建请求体
|
||||
// String requestBody = String.format(
|
||||
// "{\"appkey\":\"%s\",\"text\":\"%s\",\"voice\":\"zhifeng\",\"format\":\"MP3\",\"sample_rate\":24000,\"volume\":50,\"speech_rate\":0,\"pitch_rate\":0}",
|
||||
// appKey,
|
||||
// text.replace("\"", "\\\"")
|
||||
// text.replace("\"", "\\\"")zhide
|
||||
// );
|
||||
|
||||
// String token = refreshAccessToken();
|
||||
String requestBody = " {\n" +
|
||||
" \"appkey\":\""+appKey+"\",\n" +
|
||||
" \"voice\":\"zhifeng\",\n" +
|
||||
" \"text\":\""+text+"\",\n" +
|
||||
" \"token\":\""+token+"\",\n" +
|
||||
" \"format\":\"mp3\"\n" +
|
||||
" \"volume\": 100,\n" +
|
||||
" \"pitch_rate\": 0,\n" +
|
||||
" \"format\":\""+fileSuffix+"\"\n" +
|
||||
" }";
|
||||
|
||||
URL url = new URL(endpoint);
|
||||
URL url = new URL(BASE_URL);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 设置请求方法和头部
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
// conn.setRequestProperty("Authorization", buildAuthorization());
|
||||
conn.setRequestProperty("Authorization", buildAuthorization());
|
||||
conn.setRequestProperty("Content-Length", String.valueOf(requestBody.getBytes().length));
|
||||
conn.setDoOutput(true);
|
||||
|
||||
@ -72,13 +95,29 @@ public class HttpTtsClient {
|
||||
throw new IOException("HTTP请求失败,状态码: " + responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建授权头部
|
||||
*/
|
||||
private String buildAuthorization() {
|
||||
// 实际实现需要根据阿里云API规范构建签名
|
||||
// 这里是简化示例
|
||||
return "Bearer " + generateAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成访问令牌
|
||||
*/
|
||||
private String generateAccessToken() {
|
||||
// 实际实现需要调用阿里云获取token的API
|
||||
return "your-access-token";
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存MP3到文件
|
||||
*/
|
||||
public String saveMp3ToFile(String text, String outputPath) throws IOException {
|
||||
byte[] mp3Data = synthesizeTextToMp3(text);
|
||||
byte[] mp3Data = synthesizeTextToMp3(text,"mp3");
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
||||
fos.write(mp3Data);
|
||||
|
||||
Reference in New Issue
Block a user