日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

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