Java SDK

更新时间:
复制 MD 格式

This topic describes how to use the Java software development kit (SDK) for Alibaba Cloud Voice Service. It explains how to install the SDK and provides code examples.

Notes

Before you use the SDK, we recommend that you review the API reference. For more information, see API reference.

Download and installation

Download the latest version of the SDK, the nls-common-sdk-demos package, from the Maven server.

<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>

Key interfaces

  • NlsClient: The voice processing client used to process voice tasks. This client is thread-safe. We recommend that you create only one global instance.

  • CommonRequest: The general request class used to set request parameters and send requests and audio data. This class is not thread-safe.

  • CommonRequestListener: The general result listener class used to listen for detection results. This class is not thread-safe.

    Note

    Guidelines for SDK calls:

    • The NlsClient uses the Netty framework, and creating an NlsClient object is a time-consuming and resource-intensive operation. After an object is created, it can be reused. The creation and shutdown of the NlsClient instance must correspond to your program's lifecycle.

    • Do not reuse CommonRequest objects. Each task requires a new CommonRequest object. For example, to process N audio files as N separate tasks, you must create N CommonRequest objects.

    • Each CommonRequestListener object corresponds to a single CommonRequest object. Do not use the same listener for multiple request objects. If you do, you will not be able to distinguish between the tasks.

    • The Java SDK depends on the Netty network library. If your application also uses Netty, you must update your Netty version to 4.1.17.Final or later.

Sample code

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 LidDemoApplication {

    public static final String TOKEN = "default";

    public static final String APPKEY = "default";


    private static final String NAMESPACE = "LanguageIdentification";

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

    private static final int SAMPLE_RATE = 8000;

    private static final int CHUNK_DURATION = 500;

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

        InputStream stream = LidDemoApplication.class.getResourceAsStream("/8k-test.wav");

        CommonRequestListener listener = getListener();
        CommonRequest request = new CommonRequest(client, listener, NAMESPACE);
        request.setAppKey(APPKEY);
        request.addCustomedParam("format", "wav");
        request.addCustomedParam("sample_rate", SAMPLE_RATE);
        request.addCustomedParam("language_type", "mandenglcant");
        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;
    }
}