4. 语音转文本一句话识别(可传url)

4.1 需要依赖
<!-- 统一版本管理中-->            
<tencentcloudapi.version>3.1.317</tencentcloudapi.version>
<!--腾讯语音识别-->
<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
	<version>${tencentcloudapi.version}</version>
</dependency>
<!--然后在需要用到的地方引入这个-->
<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
4.2 使用
package com.smart.hnj.mobile.util;
import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.SentenceRecognitionRequest;
import com.tencentcloudapi.asr.v20190614.models.SentenceRecognitionResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
@Slf4j
public class VoiceToTextUtil {
    public static String VoiceUrlToText(String url) {
            String secretId = TencentSecret.SECRET_ID;
	        String secretKey = TencentSecret.SECRET_KEY;
        try {
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            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);
            SentenceRecognitionRequest req = new SentenceRecognitionRequest();
            req.setProjectId(0L);
            req.setSubServiceType(2L);
            req.setEngSerViceType("16k_zh");
            req.setSourceType(0L);
            req.setUrl(url);
            req.setVoiceFormat("mp3");
            req.setUsrAudioKey(uuid);
            SentenceRecognitionResponse resp = client.SentenceRecognition(req);
            String result = resp.getResult();
            if (StringUtils.isNotBlank(result)) {
                return result;
            }
            return null;
        } catch (TencentCloudSDKException e) {
            log.info("腾讯云接口请求错误,错误信息:{}", e.toString());
            return null;
        }
    }
}
4.3 参考api地址
https://console.cloud.tencent.com/api/explorer?Product=asr&Version=2019-06-14&Action=SentenceRecognition&SignVersion=
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐