使用阿里云录音文件识别(java)

使用阿里云录音文件识别(java)

大家好,这里介绍如何使用阿里云提供的录音文件识别的接口。

1…首先注册一个阿里云账号密码
在这里插入图片描述
注册完后,你会得到一个AccessKeyIdAccessKeySecret

2.将要识别的录音文件,上传到阿里云oss云存储服务器上,因为这个接口识别的文件必须提交基于HTTP可访问的URL地址,不支持提交本地文件。然后复制下来文件生成url地址,用于后面接口调用。
所以这里你需要开通oss云存储服务器,这里可以参考官方文档。(https://www.aliyun.com/product/oss?spm=a2c4g.11186623.2.22.78151f8dK5FB0t)
在这里插入图片描述
3.你需要到阿里云智能语音管控台,创建一个项目,并在项目中选择目标录音文件适合的场景和模型。官方文档说明(https://help.aliyun.com/document_detail/72214.html?spm=a2c4g.11186623.2.18.1c731f8dDPxbDc)这里完成后,会有一个项目的appKey

4.这时可以写Demo调用接口了
4.1创建一个maven项目,导入依赖:

<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
```java
4.2写调用接口代码:

```java
package com.suyu.websocket.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class FileTransJavaDemo {

    /**
     * 地域ID 常量内容,请勿改变
     */
    public static final String REGIONID = "cn-shanghai";
    public static final String ENDPOINTNAME = "cn-shanghai";
    public static final String PRODUCT = "nls-filetrans";
    public static final String DOMAIN = "filetrans.cn-shanghai.aliyuncs.com";
    public static final String API_VERSION = "2018-08-17";
    public static final String POST_REQUEST_ACTION = "SubmitTask";
    public static final String GET_REQUEST_ACTION = "GetTaskResult";
    /**
     * 参数设置Key 常量内容,请勿改变
     */
    public static final String KEY_APP_KEY = "app_key";
    public static final String KEY_FILE_LINK = "file_link";
    public static final String KEY_TASK = "Task";
    public static final String KEY_TASK_ID = "TaskId";
    public static final String KEY_STATUS_TEXT = "StatusText";



    public static JSONObject getInfo() throws Exception{
       /* if (args.length < 3) {
            System.err.println("FileTransJavaDemo need params: <AccessKey Id> <AccessKey Secret> <app-key>");
        }*/
        final String accessKeyId = "你的accessKeyId";
        final String accessKeySecret = "你的accessKeySecret";
        final String appKey = "你的appKey";
        /**
         * 阿里云鉴权client
         */
        IAcsClient client;
        // 设置endpoint
        DefaultProfile.addEndpoint(ENDPOINTNAME, REGIONID, PRODUCT, DOMAIN);
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, accessKeyId, accessKeySecret);
        client = new DefaultAcsClient(profile);
        /**
         * 创建CommonRequest 设置请求参数
         */
        CommonRequest postRequest = new CommonRequest();
        // 设置域名
        postRequest.setDomain(DOMAIN);
        // 设置API的版本号,格式为YYYY-MM-DD
        postRequest.setVersion(API_VERSION);
        // 设置action
        postRequest.setAction(POST_REQUEST_ACTION);
        // 设置产品名称
        postRequest.setProduct(PRODUCT);
        /**
         * 设置录音文件识别请求参数,以JSON字符串的格式设置到请求的Body中
         */
        JSONObject taskObject = new JSONObject();
        // 设置app_key
        taskObject.put(KEY_APP_KEY, appKey);
        // 设置音频文件访问链接
        taskObject.put(KEY_FILE_LINK, "你访问存储oss云服务器目标文件所在url地址");
        String task = taskObject.toJSONString();
        // 设置以上JOSN字符串为Body参数
        postRequest.putBodyParameter(KEY_TASK, task);
        // 设置为POST方式的请求
        postRequest.setMethod(MethodType.POST);
        /**
         * 提交录音文件识别请求
         */
        // 获取录音文件识别请求任务的ID,以供识别结果查询使用
        String taskId = "";
        CommonResponse postResponse = client.getCommonResponse(postRequest);
        if (postResponse.getHttpStatus() == 200) {
            JSONObject result = JSONObject.parseObject(postResponse.getData());
            String statusText = result.getString(KEY_STATUS_TEXT);
            if (statusText.equals("SUCCESS")) {
                System.out.println("录音文件识别请求成功响应: " + result.toJSONString());
                taskId = result.getString(KEY_TASK_ID);
            } else {
                System.err.println("录音文件识别请求失败: " + postResponse.getData());
                return null;
            }
        } else {
            System.err.println("录音文件识别请求失败,Http错误码:" + postResponse.getHttpStatus());
            System.err.println("录音文件识别请求失败响应:" + postResponse.getData());
            return null;
        }
        /**
         * 创建CommonRequest 设置任务ID
         */
        CommonRequest getRequest = new CommonRequest();
        // 设置域名
        getRequest.setDomain(DOMAIN);
        // 设置API版本
        getRequest.setVersion(API_VERSION);
        // 设置action
        getRequest.setAction(GET_REQUEST_ACTION);
        // 设置产品名称
        getRequest.setProduct(PRODUCT);
        // 设置任务ID为查询参数
        getRequest.putQueryParameter(KEY_TASK_ID, taskId);
        // 设置为GET方式的请求
        getRequest.setMethod(MethodType.GET);
        /**
         * 提交录音文件识别结果查询请求
         * 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述为“SUCCESS”、“SUCCESS_WITH_NO_VALID_FRAGMENT”,或者为错误描述,则结束轮询。
         */
        String statusText = "";
        JSONObject result=null;
        while (true) {
            CommonResponse getResponse = client.getCommonResponse(getRequest);
            if (getResponse.getHttpStatus() != 200) {
                System.err.println("识别结果查询请求失败,Http错误码:" + getResponse.getHttpStatus());
                System.err.println("识别结果查询请求失败:" + getResponse.getData());
                break;
            }
             result = JSONObject.parseObject(getResponse.getData());

            System.out.println("识别查询结果:" + result.toJSONString());
            statusText = result.getString(KEY_STATUS_TEXT);
            if (statusText.equals("RUNNING") || statusText.equals("QUEUEING")) {
                // 继续轮询
                Thread.sleep(3000);
            } else {
                break;
            }

        }
        if (statusText.equals("SUCCESS") || statusText.equals("SUCCESS_WITH_NO_VALID_FRAGMENT")) {
            System.out.println("录音文件识别成功!");

        } else {
            System.err.println("录音文件识别失败!");
        }
        return result;
        }
    }


5.接下来就是启动项目,调用接口,结果如下:
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值