语音管理

This commit is contained in:
2026-02-02 16:06:11 +08:00
parent 6fd7f8ca94
commit 2e575f78b1
20 changed files with 775 additions and 6 deletions

View File

@ -113,6 +113,21 @@ public class AlibabaTTSUtil {
DEFAULT_VOLUME, DEFAULT_SPEECH_RATE, DEFAULT_PITCH_RATE);
}
public byte[] synthesizeTextToMp3(String text){
try {
// 获取访问令牌
String token = getValidAccessToken();
// 使用HTTP方式调用
HttpTtsClient httpClient = new HttpTtsClient(appkey,token);
return httpClient.synthesizeTextToMp3(text);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成语音文件 - 完整版(支持所有参数调节)
*

View File

@ -0,0 +1,89 @@
package com.fuyuanshen.equipment.utils;
import com.alibaba.nls.client.AccessToken;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import static cn.dev33.satoken.SaManager.log;
public class HttpTtsClient {
private String appKey;
private String token;
/**
* 阿里云TTS服务基础URL
*/
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;
this.token = token;
}
/**
* 使用HTTP POST方式调用阿里云TTS服务生成MP3格式语音
*/
public byte[] synthesizeTextToMp3(String text) 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("\"", "\\\"")
// );
String requestBody = " {\n" +
" \"appkey\":\""+appKey+"\",\n" +
" \"text\":\""+text+"\",\n" +
" \"token\":\""+token+"\",\n" +
" \"format\":\"mp3\"\n" +
" }";
URL url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方法和头部
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
// conn.setRequestProperty("Authorization", buildAuthorization());
conn.setRequestProperty("Content-Length", String.valueOf(requestBody.getBytes().length));
conn.setDoOutput(true);
// 发送请求体
try (OutputStream os = conn.getOutputStream()) {
os.write(requestBody.getBytes("UTF-8"));
}
// 读取响应
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// 读取音频数据
try (InputStream is = conn.getInputStream()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
} else {
throw new IOException("HTTP请求失败状态码: " + responseCode);
}
}
/**
* 保存MP3到文件
*/
public String saveMp3ToFile(String text, String outputPath) throws IOException {
byte[] mp3Data = synthesizeTextToMp3(text);
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
fos.write(mp3Data);
}
return outputPath;
}
}

View File

@ -0,0 +1,35 @@
package com.fuyuanshen.equipment.utils;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3File;
import java.io.File;
public class Mp3Duration {
/**
* 获取MP3文件的播放时长
*
* @param filePath MP3文件路径
* @return 播放时长(秒)
*/
public static int getMp3Duration(String filePath) {
try {
File file = new File(filePath);
MP3File mp3File = (MP3File) AudioFileIO.read(file);
return mp3File.getMP3AudioHeader().getTrackLength();
} catch (Exception e) {
e.printStackTrace();
return -1; // 错误时返回-1
}
}
public static void main(String[] args) {
String filePath = "D:\\http_output.mp3"; // 替换为实际路径
int duration = getMp3Duration(filePath);
if (duration != -1) {
System.out.println("MP3时长: " + duration + "");
} else {
System.out.println("无法获取MP3时长");
}
}
}