语音管理
This commit is contained in:
@ -0,0 +1,84 @@
|
||||
package com.fuyuanshen.app.http;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
public class ApacheHttpTtsClient {
|
||||
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String appKey;
|
||||
|
||||
public ApacheHttpTtsClient(String accessKeyId, String accessKeySecret, String appKey) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
this.accessKeySecret = accessKeySecret;
|
||||
this.appKey = appKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Apache HttpClient调用TTS服务
|
||||
*/
|
||||
public byte[] synthesizeTextToMp3(String text) throws IOException {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
try {
|
||||
HttpPost httpPost = new HttpPost("https://nls-gateway.cn-shanghai.aliyuncs.com/v1/tts");
|
||||
|
||||
// 设置请求头
|
||||
httpPost.setHeader("Content-Type", "application/json");
|
||||
httpPost.setHeader("Authorization", "Bearer " + getAccessToken());
|
||||
|
||||
// 设置请求体
|
||||
String jsonPayload = String.format(
|
||||
"{\"appkey\":\"%s\",\"text\":\"%s\",\"voice\":\"zhifeng\",\"format\":\"MP3\",\"sample_rate\":24000,\"volume\":50,\"speech_rate\":0,\"pitch_rate\":0}",
|
||||
appKey,
|
||||
text.replace("\"", "\\\"")
|
||||
);
|
||||
|
||||
StringEntity entity = new StringEntity(jsonPayload, "UTF-8");
|
||||
httpPost.setEntity(entity);
|
||||
|
||||
// 执行请求
|
||||
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||||
try {
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
// 读取响应数据(Base64编码的音频数据)
|
||||
String responseStr = EntityUtils.toString(responseEntity);
|
||||
|
||||
// 如果返回的是Base64编码的数据
|
||||
if (responseStr.startsWith("data:audio/mp3;base64,")) {
|
||||
String base64Data = responseStr.substring("data:audio/mp3;base64,".length());
|
||||
return Base64.getDecoder().decode(base64Data);
|
||||
} else {
|
||||
// 直接返回音频数据
|
||||
return EntityUtils.toByteArray(responseEntity);
|
||||
}
|
||||
} else {
|
||||
throw new IOException("请求失败: " + response.getStatusLine().getStatusCode());
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} finally {
|
||||
httpClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取访问令牌
|
||||
*/
|
||||
private String getAccessToken() {
|
||||
// 实现获取阿里云访问令牌的逻辑
|
||||
return "your-access-token";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,130 @@
|
||||
package com.fuyuanshen.app.http;
|
||||
|
||||
import com.alibaba.nls.client.AccessToken;
|
||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.time.Duration;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static cn.dev33.satoken.SaManager.log;
|
||||
|
||||
public class HttpTtsClient {
|
||||
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String appKey;
|
||||
/**
|
||||
* 阿里云TTS服务基础URL
|
||||
*/
|
||||
private static final String BASE_URL = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
|
||||
|
||||
public HttpTtsClient(String accessKeyId, String accessKeySecret, String appKey) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
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";
|
||||
|
||||
// 构建请求体
|
||||
// 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 token = refreshAccessToken();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建授权头部
|
||||
*/
|
||||
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);
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
||||
fos.write(mp3Data);
|
||||
}
|
||||
|
||||
return outputPath;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.fuyuanshen.app.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HttpTtsExample {
|
||||
|
||||
/**
|
||||
* appKey: lbGuq5K5bEH4uxmT
|
||||
* akId: LTAI5t66moCkhNC32TDJ5ReP
|
||||
* akSecret: 2F3sdoBJ08bYvJcuDgSkLnJwGXsvYH
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String accessKeyId = "LTAI5t66moCkhNC32TDJ5ReP";
|
||||
String accessKeySecret = "2F3sdoBJ08bYvJcuDgSkLnJwGXsvYH";
|
||||
String appKey = "lbGuq5K5bEH4uxmT";
|
||||
|
||||
try {
|
||||
// 使用HTTP方式调用
|
||||
HttpTtsClient httpClient = new HttpTtsClient(accessKeyId, accessKeySecret, appKey);
|
||||
|
||||
String text = "大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。";
|
||||
String outputPath = "D:\\http_output9.mp3";
|
||||
|
||||
String resultFile = httpClient.saveMp3ToFile(text, outputPath);
|
||||
System.out.println("MP3音频已保存至: " + resultFile);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package com.fuyuanshen.app.http;
|
||||
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class OkHttpTtsClient {
|
||||
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String appKey;
|
||||
|
||||
public OkHttpTtsClient(String accessKeyId, String accessKeySecret, String appKey) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
this.accessKeySecret = accessKeySecret;
|
||||
this.appKey = appKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用OkHttp调用TTS服务
|
||||
*/
|
||||
public byte[] synthesizeTextToMp3(String text) throws IOException {
|
||||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
String jsonPayload = String.format(
|
||||
"{\"appkey\":\"%s\",\"text\":\"%s\",\"voice\":\"zhifeng\",\"format\":\"MP3\",\"sample_rate\":24000,\"volume\":50,\"speech_rate\":0,\"pitch_rate\":0}",
|
||||
appKey,
|
||||
text.replace("\"", "\\\"")
|
||||
);
|
||||
|
||||
RequestBody body = RequestBody.create(JSON,jsonPayload);
|
||||
Request request = new Request.Builder()
|
||||
.url("https://nls-gateway.cn-shanghai.aliyuncs.com/v1/tts")
|
||||
.post(body)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("Authorization", "Bearer " + getAccessToken())
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("HTTP请求失败: " + response.code());
|
||||
}
|
||||
|
||||
ResponseBody responseBody = response.body();
|
||||
if (responseBody != null) {
|
||||
return responseBody.bytes();
|
||||
} else {
|
||||
throw new IOException("响应体为空");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getAccessToken() {
|
||||
// 实现获取访问令牌的逻辑
|
||||
return "your-access-token";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.fuyuanshen.app.http;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 获取阿里云TTS服务访问令牌
|
||||
*/
|
||||
public class TokenClient {
|
||||
|
||||
public static String getAccessToken(String accessKeyId, String accessKeySecret) throws IOException {
|
||||
String endpoint = "https://nls-meta.cn-shanghai.aliyuncs.com/v1/token";
|
||||
|
||||
URL url = new URL(endpoint);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
conn.setDoOutput(true);
|
||||
|
||||
// 发送认证信息
|
||||
String params = String.format("grant_type=client_credentials&client_id=%s&client_secret=%s",
|
||||
accessKeyId, accessKeySecret);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(params.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
try (InputStream is = conn.getInputStream();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
|
||||
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
|
||||
// 解析响应获取token(实际需要使用JSON解析库)
|
||||
return response.toString().replaceAll(".*\"access_token\":\"([^\"]+)\".*", "$1");
|
||||
}
|
||||
} else {
|
||||
throw new IOException("获取令牌失败,状态码: " + responseCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user