Java腾讯文本转语音(简洁版)

一、基础

基于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());
        }
    }
}

三、参考资料

  1. 本页面案例参考腾讯云官方:腾讯语音合成快速接入
  2. TextToVoiceRequest 对象的一些其它参数可以参考上方链接的参数说明页面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想在Java使用腾讯云语音识别API,可以借助腾讯云提供的Java SDK来实现。以下是一些基本的使用步骤: 1. 首先,您需要在腾讯云官网上开通语音识别服务,并获取到相应的SecretID和SecretKey。 2. 接下来,您需要在Java项目中引入腾讯云Java SDK,可以通过Maven或Gradle等构建工具添加依赖。 3. 在Java代码中,通过创建一个语音识别客户端对象来调用语音识别API。您需要在创建客户端对象时指定API的地域、SecretID和SecretKey等参数,例如: ``` import com.tencentcloudapi.asr.v20190614.AsrClient; import com.tencentcloudapi.asr.v20190614.models.*; AsrClient client = new AsrClient(new Credential("your-secret-id", "your-secret-key"), "ap-guangzhou"); ``` 4. 调用语音识别API进行语音识别,例如: ``` // 创建API请求对象 CreateRecTaskRequest request = new CreateRecTaskRequest(); request.setEngineModelType("16k_zh"); request.setChannelNum(1); request.setResTextFormat(0); request.setDataLen(data.length); request.setData(new String(Base64.encodeBase64(data))); // 发送API请求并获取响应 CreateRecTaskResponse response = client.CreateRecTask(request); // 解析响应结果 if (response != null && response.getData() != null) { String result = new String(Base64.decodeBase64(response.getData())); System.out.println(result); } ``` 以上代码仅是一个简单的示例,具体实现还需要根据您的实际需求进行调整。同时,腾讯云还提供了详细的API文档和示例代码供您参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值