一、基础
基于springboot
二、正文
2.1 引入依赖
<!--腾讯文本转语音-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-speech-sdk-java</artifactId>
<version>1.0.12</version>
</dependency>
2.2 工具类
用来解析返回的字符串数组
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
/**
* 简述:
* 日期: 2022/12/29
* 作者:
*/
public class VoiceUtils {
public static void convertByteStrStrToVoice(String byteArrStr, String path, String fileName){
File judgeExists = new File(path);
if (!judgeExists.exists()){
// 如果路径不存在就创建
judgeExists.mkdirs();
}
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File newFile = new File(path + fileName);
try {
if (!newFile.exists()) {
newFile.createNewFile();
}
byte[] bytes = Base64.getDecoder().decode(byteArrStr);
fos = new java.io.FileOutputStream(newFile);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
System.out.println("语音报存到本地出错,请检查:" + e.getMessage());
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
System.out.println("语音报存到本地出错,请检查:" + e.getMessage());
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("语音报存到本地出错,请检查:" + e.getMessage());
}
}
}
}
}
2.3 主要代码
import com.jisudz.ai.virtual.soul.utils.VoiceUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tts.v20190823.TtsClient;
import com.tencentcloudapi.tts.v20190823.models.*;
import java.util.UUID;
/**
1. 简述:
2. 日期: 2022/12/29
3. 作者:
*/
public class test {
public static void main(String[] args) {
try{
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
Credential cred = new Credential("secretId", "secretKey");
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("tts.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
TtsClient client = new TtsClient(cred, "ap-beijing", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象,规定语音的一些参数
TextToVoiceRequest req = new TextToVoiceRequest();
// 设置发音人
req.setVoiceType(1017L);
// 转换文本
req.setText("测试腾讯发音");
String session = UUID.randomUUID().toString();
// 设置session
req.setSessionId(session);
// 返回的resp是一个TextToVoiceResponse的实例,与请求对象对应
TextToVoiceResponse resp = client.TextToVoice(req);
// 获取返回的语音字符数组
String audio = resp.getAudio();
// 转换字符数组
VoiceUtils.convertByteStrStrToVoice(audio,"D:\\","222.wav");
// 输出json格式的字符串回包
System.out.println(TextToVoiceResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
三、参考资料
- 本页面案例参考腾讯云官方:腾讯语音合成快速接入
- TextToVoiceRequest 对象的一些其它参数可以参考上方链接的参数说明页面