Java SDK

更新时间:
复制 MD 格式

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

Notes

Before you use the SDK, read the API reference. For more information, see API reference.

Download and installation

You can download the latest version of the SDK from the Maven server. To try the speaker verification feature without building a project from scratch, you can also download a complete sample that includes the integrated 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>

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 generic request class. You can use this class to set request parameters and send requests and audio data. This class is not thread-safe.

  • CommonRequestListener: The generic result listener class that listens for recognition results. This class is not thread-safe.

    Note

    Note the following when you make SDK calls:

    • NlsClient uses the Netty framework. Creating an NlsClient object consumes time and resources. After an object is created, it can be reused. The creation and shutdown of the NlsClient instance should align with your program's lifecycle.

    • CommonRequest objects cannot be reused. Each task requires one CommonRequest object. For example, to process N audio files in N tasks, you must create N CommonRequest objects.

    • Each CommonRequestListener object corresponds to a single CommonRequest object. Do not use the same listener for different requests. Otherwise, the tasks cannot be distinguished.

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

Code example

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;

public class VprloginApplyDigitDemo {

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

    public static void main(String[] args) {
        String appkey = "";
        String token = "";

        NlsClient client = new NlsClient(token);
        try {
            CommonRequestListener listener = getListener();
            CommonRequest commonRequest = new CommonRequest(client, listener, "SpeakerVerification");
            commonRequest.setAppKey(appkey);
            commonRequest.addCustomedParam("action", "ApplyDigit");
            commonRequest.addCustomedParam("speaker_id", "test_speaker");

            commonRequest.start();
            commonRequest.stop();
            commonRequest.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        client.shutdown();
    }
}