java调用腾讯云接口-第二篇(录音文件识别)

Posted 叶梓啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java调用腾讯云接口-第二篇(录音文件识别)相关的知识,希望对你有一定的参考价值。

  • 需要先请求任务,再请求结果,隔一段时间才能去取到结果

2.语音转文本(任务)

2.1 需要依赖

<!-- 统一版本管理中-->            
<tencentcloud.version>3.1.317</tencentcloud.version>
<!--腾讯语音识别-->
<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
	<version>${tencentcloud.version}</version>
</dependency>
<!--然后在需要用到的地方引入这个-->
<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>

2.2 使用

//分段,第一段获取taskId,第二段获取文本
//获取语音转文本任务id
    private Long getVoiceTaskId(String url) {
//String url = "http://www.luyin.com/upload/duanjuyuyin1206010.mp3";
        try {
	        String secretId = TencentSecret.SECRET_ID;
	        String secretKey = TencentSecret.SECRET_KEY;
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            AsrClient client = new AsrClient(cred, "", clientProfile);
            CreateRecTaskRequest req = new CreateRecTaskRequest();
            req.setEngineModelType("16k_zh");//中文语音通用
            req.setChannelNum(1L);//单声道
            req.setResTextFormat(0L);//识别结果文本(含分段时间戳)
            req.setSourceType(0L);//0:语音 URL
            req.setUrl(url);//音频地址
            CreateRecTaskResponse resp = client.CreateRecTask(req);
            String voiceTaskResult = CreateRecTaskResponse.toJsonString(resp);
            VoiceReadResult voiceReadResult = JSON.parseObject(voiceTaskResult, VoiceReadResult.class);
            if (voiceReadResult != null) {
                Object taskData = voiceReadResult.getData();
                VoiceReadResult res = (VoiceReadResult) taskData;
                Long taskId = res.getData().getTaskId();
                return taskId;
            }
        } catch (TencentCloudSDKException e) {
            system.out.println("腾讯音频转化任务接口调用失败,错误信息:"+e);
            return null;
        }
        return null;
    }

    //获取语音转文本查询结果
    private String getVoiceText(Long taskId) {
        try {
	        String secretId = TencentSecret.SECRET_ID;
	        String secretKey = TencentSecret.SECRET_KEY;
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            AsrClient client = new AsrClient(cred, "", clientProfile);
            DescribeTaskStatusRequest req = new DescribeTaskStatusRequest();
            req.setTaskId(taskId);
            DescribeTaskStatusResponse resp = client.DescribeTaskStatus(req);
            String voiceTextResult = CreateRecTaskResponse.toJsonString(resp);
            VoiceReadResult voiceReadResult = JSON.parseObject(voiceTextResult, VoiceReadResult.class);
            if (voiceReadResult != null) {
                Object textData = voiceReadResult.getData();
                VoiceReadResult res = (VoiceReadResult) textData;
                String text = res.getData().getResult();
                return text;
            }
        } catch (TencentCloudSDKException e) {
            system.out.println("腾讯音频转化任务接口调用失败,错误信息:"+ e);
            return null;
        }
        return null;
    }

2.3 参考api地址

#语音转文本请求
- https://console.cloud.tencent.com/api/explorer?Product=asr&Version=2019-06-14&Action=CreateRecTask&SignVersion=
- https://cloud.tencent.com/document/product/1093/37823
#语音转文本结果
- https://console.cloud.tencent.com/api/explorer?Product=asr&Version=2019-06-14&Action=DescribeTaskStatus&SignVersion=
- https://cloud.tencent.com/document/product/1093/37822

2.4 封装的类

import lombok.Data;
@Data
public class VoiceReadResult {
    private VoiceReadResultData data;
    private String requestId;
}
import lombok.Data;
@Data
public class VoiceReadResultData {
    private Long taskId;
    private Long status;
    private String statusStr;
    private String result;
}

以上是关于java调用腾讯云接口-第二篇(录音文件识别)的主要内容,如果未能解决你的问题,请参考以下文章

Python调用腾讯API进行银行卡识别

Python调用腾讯API进行银行卡识别

Python 调用腾讯云接口批量识别图片中指定位置的信息,并保存到excel

Python 调用腾讯云接口批量识别图片中指定位置的信息,并保存到excel

本地录音调用百度语音识别接口

Java集成腾讯云的短信验证码