Java SDK

本文介绍如何使用阿里云智能语音服务提供的Java SDK,包括SDK的安装方法及SDK代码示例。

注意事项

在使用SDK前,请先阅读接口说明,详情请参见接口说明

下载安装

从Maven服务器下载最新版本SDK,下载nls-common-sdk-demos

<dependency>
  <groupId>com.alibaba.nls</groupId>
  <artifactId>nls-sdk-common</artifactId>
  <version>2.2.1</version>
</dependency>
<dependency>
  <groupId>com.alibaba.nls</groupId>
  <artifactId>nls-sdk-request</artifactId>
  <version>2.2.1</version>
</dependency>

关键接口

  • NlsClient:语音处理客户端,利用该客户端可以处理语音任务。该客户端为线程安全,建议全局仅创建一个实例。

  • CommonRequest:通用请求类,通过该接口设置请求参数,发送请求及声音数据。非线程安全。

  • CommonRequestListener:通用结果监听类,监听识别结果。非线程安全。

    说明

    SDK调用注意事项:

    • NlsClient使用了Netty框架,NlsClient对象的创建会消耗一定时间和资源,一经创建可以重复使用。建议调用程序将NlsClient的创建和关闭与程序本身的生命周期相结合。

    • CommonRequest对象不可重复使用,一个任务对应一个CommonRequest对象。例如,N个音频文件要进行N次任务,创建N个CommonRequest对象。

    • CommonRequestListener对象和CommonRequest对象是一一对应的,不能在不同CommonRequest对象使用同一个CommonRequestListener对象,否则不能将各任务区分开。

    • Java SDK依赖Netty网络库,如果您的应用依赖Netty,其版本需更新至4.1.17.Final及以上。

示例代码

package com.alibaba.nls.demo;

import com.alibaba.nls.client.protocol.NlsClient;
import com.alibaba.nls.client.protocol.commonrequest.CommonRequest;
import com.alibaba.nls.client.protocol.commonrequest.CommonRequestListener;
import com.alibaba.nls.client.protocol.commonrequest.CommonRequestResponse;

import java.io.InputStream;
import java.util.Arrays;

public class AudioEventDetectionDemo {

    public static final String TOKEN = "default";      //获取Token具体操作,请参见:https://help.aliyun.com/document_detail/450514.html

    public static final String APPKEY = "default";      //获取AppKey请前往控制台:https://nls-portal.console.aliyun.com/applist

    private static final String NAMESPACE = "AudioEventDetection";

    private static final String URL = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1";

    private static final int SAMPLE_RATE = 16000;

    private static final int CHUNK_DURATION = 200;

    public static void main(String[] args) throws Exception {
        NlsClient client = new NlsClient(URL, TOKEN);

        InputStream stream = AudioEventDetectionDemo.class.getResourceAsStream("/test.pcm");

        CommonRequestListener listener = getListener();
        CommonRequest request = new CommonRequest(client, listener, NAMESPACE);
        request.setAppKey(APPKEY);
        request.addCustomedParam("format", "pcm");
        request.addCustomedParam("sample_rate", 16000);
        request.start();
        int chunkSize = SAMPLE_RATE * 2 / 1000 * CHUNK_DURATION;
        byte[] data = new byte[chunkSize];
        while (true) {
            int len = stream.read(data);
            if (len < 0) {
                break;
            }
            if (len > 0) {
                request.send(Arrays.copyOf(data, len));
            }
            Thread.sleep(CHUNK_DURATION / 10);
        }
        request.stop();

        client.shutdown();
    }

    private static CommonRequestListener getListener() {
        CommonRequestListener listener = new CommonRequestListener() {
            @Override
            public void onStarted(CommonRequestResponse response) {
                System.out.println(
                    "onStarted, taskId: " + response.getTaskId() + ", header: " + response.header + ", payload: "
                        + response.payload);
            }

            @Override
            public void onEvent(CommonRequestResponse response) {
                System.out.println(
                    "onEvent, taskId: " + response.getTaskId() + ", header: " + response.header + ", payload: "
                        + response.payload);
            }

            @Override
            public void onStopped(CommonRequestResponse response) {
                System.out.println(
                    "onStopped, taskId: " + response.getTaskId() + ", header: " + response.header + ", payload: "
                        + response.payload);
            }

            @Override
            public void onFailed(CommonRequestResponse response) {
                System.out.println(
                    "onFailed, taskId: " + response.getTaskId() + ", header: " + response.header + ", payload: "
                        + response.payload);
            }
        };
        return listener;
    }
}