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

Java Demo

本文介紹如何使用阿里云智能語音服務提供的Java SDK,包括SDK的安裝方法及SDK代碼示例。

前提條件

  • 使用SDK前,請先閱讀接口說明,詳情請參見接口說明。

  • 已開通智能語音交互并獲取AccessKey ID和AccessKey Secret,詳情請參見從這里開始。

SDK說明

錄音文件識別的Java示例使用了阿里云Java SDK的CommonRequest提交錄音文件識別請求和識別結果查詢,采用的是RPC風格的POP API調用。阿里云Java SDK CommonRequest的使用方法請參見使用CommonRequest進行調用

重要

阿里云Java SDK不支持Android開發。

Java依賴

您只需依賴阿里云Java SDK的核心庫與阿里巴巴開源庫fastjson。阿里云Java SDK的核心庫版本支持3.5.0及以上(如果版本在4.0.0及以上,需要根據錯誤提示補充對應的第三方依賴)。

<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.83</version>
</dependency>

示例說明

  • 下載nls-sample-16k.wav。示例中使用的WAV錄音文件為PCM編碼格式16000 Hz采樣率,模型設置為通用模型。

  • 調用接口前,需配置環境變量,通過環境變量讀取訪問憑證。智能語音交互的AccessKey ID、AccessKey Secret和AppKey的環境變量名:ALIYUN_AK_IDALIYUN_AK_SECRET、NLS_APP_KEY。

鑒權

通過傳入阿里云賬號的AccessKey ID和AccessKey Secret(獲取方式請參見開通服務),調用阿里云Java SDK得到client,示例如下:

final String accessKeyId = System.getenv().get("ALIYUN_AK_ID");
final String accessKeySecret = System.getenv().get("ALIYUN_AK_SECRET");
/**
 * 地域ID
 */
final String regionId = "cn-shanghai";
final String endpointName = "cn-shanghai";
final String product = "SpeechFileTranscriberLite";
final String domain = "speechfiletranscriberlite.cn-shanghai.aliyuncs.com";

IAcsClient client;
// 設置endpoint
DefaultProfile.addEndpoint(endpointName, regionId, product, domain);
// 創建DefaultAcsClient實例并初始化
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
client = new DefaultAcsClient(profile);

錄音文件識別請求調用接口

Java 示例采用輪詢的方式,提交錄音文件識別請求,獲取任務ID,供后續輪詢使用。

說明

參數設置請參見接口說明,只需設置JSON字符串中的參數,其他方法的參數值保持不變。

/**
 * 創建CommonRequest 設置請求參數
 */
CommonRequest postRequest = new CommonRequest();
postRequest.setDomain("speechfiletranscriberlite.cn-shanghai.aliyuncs.com"); // 設置域名,固定值。
postRequest.setVersion("2021-12-21");         // 設置API的版本號,固定值。
postRequest.setAction("SubmitTask");          // 設置action,固定值。
postRequest.setProduct("SpeechFileTranscriberLite");      // 設置產品名稱,固定值。
// 設置錄音文件識別請求參數,以JSON字符串的格式設置到請求Body中。
JSONObject taskObject = new JSONObject();
taskObject.put("appkey", "您的appkey");    // 項目的Appkey,獲取Appkey請前往控制臺:https://nls-portal.console.aliyun.com/applist
taskObject.put("file_link", "您的錄音文件訪問鏈接");  // 設置錄音文件的鏈接
String task = taskObject.toJSONString();
postRequest.putBodyParameter("Task", task);  // 設置以上JSON字符串為Body參數。
postRequest.setMethod(MethodType.POST);      // 設置為POST方式請求。
// postRequest.setHttpContentType(FormatType.JSON);    //當aliyun-java-sdk-core 版本為4.6.0及以上時,請取消該行注釋
/**
 * 提交錄音文件識別請求
 */
String taskId = "";   // 獲取錄音文件識別請求任務的ID,以供識別結果查詢使用。
CommonResponse postResponse = client.getCommonResponse(postRequest);
if (postResponse.getHttpStatus() == 200) {
    JSONObject result = JSONObject.parseObject(postResponse.getData());
    String statusText = result.getString("StatusText");
    if ("SUCCESS".equals(statusText)) {
        System.out.println("錄音文件識別請求成功響應: " + result.toJSONString());
        taskId = result.getString("TaskId");
    }
    else {
        System.out.println("錄音文件識別請求失?。?" + result.toJSONString());
        return;
    }
}
else {
    System.err.println("錄音文件識別請求失敗,Http錯誤碼:" + postResponse.getHttpStatus());
    System.err.println("錄音文件識別請求失敗響應:" + JSONObject.toJSONString(postResponse));
    return;
}

錄音文件識別結果查詢

使用獲取的任務ID,查詢錄音文件識別的結果。

/**
 * 創建CommonRequest,設置任務ID。
 */
CommonRequest getRequest = new CommonRequest();
getRequest.setDomain("speechfiletranscriberlite.cn-shanghai.aliyuncs.com");   // 設置域名,固定值。
getRequest.setVersion("2021-12-21");             // 設置API版本,固定值。
getRequest.setAction("GetTaskResult");           // 設置action,固定值。
getRequest.setProduct("SpeechFileTranscriberLite");          // 設置產品名稱,固定值。
getRequest.putQueryParameter("TaskId", taskId);  // 設置任務ID為查詢參數。
getRequest.setMethod(MethodType.GET);            // 設置為GET方式的請求。
/**
 * 提交錄音文件識別結果查詢請求
 * 以輪詢的方式進行識別結果的查詢,直到服務端返回的狀態描述為“SUCCESS”、“SUCCESS_WITH_NO_VALID_FRAGMENT”,或者為錯誤描述,則結束輪詢。
 */
String statusText = "";
while (true) {
    CommonResponse getResponse = client.getCommonResponse(getRequest);
    if (getResponse.getHttpStatus() != 200) {
        System.err.println("識別結果查詢請求失敗,Http錯誤碼: " + getResponse.getHttpStatus());
        System.err.println("識別結果查詢請求失敗: " + getResponse.getData());
        break;
    }
    JSONObject result = JSONObject.parseObject(getResponse.getData());
    System.out.println("識別查詢結果:" + result.toJSONString());
    statusText = result.getString("StatusText");
    if ("RUNNING".equals(statusText) || "QUEUEING".equals(statusText)) {
        // 繼續輪詢
        Thread.sleep(3000);
    }
    else {
        break;
    }
}
if ("SUCCESS".equals(statusText) || "SUCCESS_WITH_NO_VALID_FRAGMENT".equals(statusText)) {
    System.out.println("錄音文件識別成功!");
}
else {
    System.err.println("錄音文件識別失??!");
}

示例代碼

調用接口前,需配置環境變量,通過環境變量讀取訪問憑證。智能語音交互的AccessKey ID、AccessKey Secret和AppKey的環境變量名:ALIYUN_AK_ID、ALIYUN_AK_SECRET、NLS_APP_KEY

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
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 = "SpeechFileTranscriberLite";
    public static final String DOMAIN = "speechfiletranscriberlite.cn-shanghai.aliyuncs.com";
    public static final String API_VERSION = "2021-12-21";
    public static final String POST_REQUEST_ACTION = "SubmitTask";
    public static final String GET_REQUEST_ACTION = "GetTaskResult";
    // 請求參數
    public static final String KEY_APP_KEY = "appkey";
    public static final String KEY_FILE_LINK = "file_link";
    public static final String KEY_VERSION = "version";
    public static final String KEY_ENABLE_WORDS = "enable_words";
    // 響應參數
    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 final String KEY_RESULT = "Result";
    // 狀態值
    public static final String STATUS_SUCCESS = "SUCCESS";
    private static final String STATUS_RUNNING = "RUNNING";
    private static final String STATUS_QUEUEING = "QUEUEING";
    // 阿里云鑒權client
    IAcsClient client;
    public FileTransJavaDemo(String accessKeyId, String accessKeySecret) {
        // 設置endpoint
        try {
            DefaultProfile.addEndpoint(ENDPOINTNAME, REGIONID, PRODUCT, DOMAIN);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // 創建DefaultAcsClient實例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, accessKeyId, accessKeySecret);
        this.client = new DefaultAcsClient(profile);
    }
    public String submitFileTransRequest(String appKey, String fileLink) {
        /**
         * 1. 創建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);
        /**
         * 2. 設置錄音文件識別請求參數,以JSON字符串的格式設置到請求Body中。
         */
        JSONObject taskObject = new JSONObject();
        // 設置appkey
        taskObject.put(KEY_APP_KEY, appKey);
        // 設置音頻文件訪問鏈接
        taskObject.put(KEY_FILE_LINK, fileLink);
        // 設置是否輸出詞信息,默認為false。
        taskObject.put(KEY_ENABLE_WORDS, true);
        String task = taskObject.toJSONString();
        System.out.println(task);
        // 設置以上JSON字符串為Body參數。
        postRequest.putBodyParameter(KEY_TASK, task);
        // 設置為POST方式的請求。
        postRequest.setMethod(MethodType.POST);
        // postRequest.setHttpContentType(FormatType.JSON);    //當aliyun-java-sdk-core 版本為4.6.0及以上時,請取消該行注釋  
        /**
         * 3. 提交錄音文件識別請求,獲取錄音文件識別請求任務的ID,以供識別結果查詢使用。
         */
        String taskId = null;
        try {
            CommonResponse postResponse = client.getCommonResponse(postRequest);
            System.err.println("提交錄音文件識別請求的響應:" + postResponse.getData());
            if (postResponse.getHttpStatus() == 200) {
                JSONObject result = JSONObject.parseObject(postResponse.getData());
                String statusText = result.getString(KEY_STATUS_TEXT);
                if (STATUS_SUCCESS.equals(statusText)) {
                    taskId = result.getString(KEY_TASK_ID);
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return taskId;
    }
    public String getFileTransResult(String taskId) {
        /**
         * 1. 創建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);
        /**
         * 2. 提交錄音文件識別結果查詢請求
         * 以輪詢的方式進行識別結果的查詢,直到服務端返回的狀態描述為“SUCCESS”或錯誤描述,則結束輪詢。
         */
        String result = null;
        while (true) {
            try {
                CommonResponse getResponse = client.getCommonResponse(getRequest);
                System.err.println("識別查詢結果:" + getResponse.getData());
                if (getResponse.getHttpStatus() != 200) {
                    break;
                }
                JSONObject rootObj = JSONObject.parseObject(getResponse.getData());
                String statusText = rootObj.getString(KEY_STATUS_TEXT);
                if (STATUS_RUNNING.equals(statusText) || STATUS_QUEUEING.equals(statusText)) {
                    // 繼續輪詢,注意設置輪詢時間間隔。
                    Thread.sleep(10000);
                }
                else {
                    // 狀態信息為成功,返回識別結果;狀態信息為異常,返回空。
                    if (STATUS_SUCCESS.equals(statusText)) {
                        result = rootObj.getString(KEY_RESULT);
                       // 狀態信息為成功,但沒有識別結果,則可能是由于文件里全是靜音、噪音等導致識別為空。
                        if(result == null) {
                            result = "";
                        }
                    }
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static void main(String args[]) throws Exception {
        final String accessKeyId = System.getenv().get("ALIYUN_AK_ID");
        final String accessKeySecret = System.getenv().get("ALIYUN_AK_SECRET");
        final String appKey = System.getenv().get("NLS_APP_KEY");      
        String fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
        FileTransJavaDemo demo = new FileTransJavaDemo(accessKeyId, accessKeySecret);
        // 第一步:提交錄音文件識別請求,獲取任務ID用于后續的識別結果輪詢。
        String taskId = demo.submitFileTransRequest(appKey, fileLink);
        if (taskId != null) {
            System.out.println("錄音文件識別請求成功,task_id: " + taskId);
        }
        else {
            System.out.println("錄音文件識別請求失??!");
            return;
        }
        // 第二步:根據任務ID輪詢識別結果。
        String result = demo.getFileTransResult(taskId);
        if (result != null) {
            System.out.println("錄音文件識別結果查詢成功:" + result);
        }
        else {
            System.out.println("錄音文件識別結果查詢失??!");
        }
    }
}

說明

如果使用回調方式,請設置如下enable_callbackcallback_url參數:

taskObject.put("enable_callback", true);
taskObject.put("callback_url", "回調地址");

回調服務示例:該服務用于回調方式獲取識別結果,假設設置回調地址為http://ip:port/filetrans/callback/result。

package com.example.filetrans;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RequestMapping("/filetrans/callback")
@RestController
public class FiletransCallBack {
    // 以4開頭的狀態碼是客戶端錯誤。
    private static final Pattern PATTERN_CLIENT_ERR = Pattern.compile("4105[0-9]*");
    // 以5開頭的狀態碼是服務端錯誤。
    private static final Pattern PATTERN_SERVER_ERR = Pattern.compile("5105[0-9]*");
    // 必須是post方式
    @RequestMapping(value = "result", method = RequestMethod.POST)
    public void GetResult(@RequestBody String body) {
        System.out.println("body: " + body);
        try {
            // 獲取JSON格式的文件識別結果。
            String result = body;
            JSONObject jsonResult = JSONObject.parseObject(result);
            // 解析并輸出相關結果內容。
            System.out.println("獲取文件中轉寫回調結果:" + result);
            System.out.println("TaskId: " + jsonResult.getString("TaskId"));
            System.out.println("StatusCode: " + jsonResult.getString("StatusCode"));
            System.out.println("StatusText: " + jsonResult.getString("StatusText"));
            Matcher matcherClient = PATTERN_CLIENT_ERR.matcher(jsonResult.getString("StatusCode"));
            Matcher matcherServer = PATTERN_SERVER_ERR.matcher(jsonResult.getString("StatusCode"));
            // 以2開頭狀態碼為正常狀態碼,回調方式正常狀態只返回“21050000”。
            if("21050000".equals(jsonResult.getString("StatusCode"))) {
                System.out.println("RequestTime: " + jsonResult.getString("RequestTime"));
                System.out.println("SolveTime: " + jsonResult.getString("SolveTime"));
                System.out.println("BizDuration: " + jsonResult.getString("BizDuration"));
                System.out.println("Result.Sentences.size: " +
                    jsonResult.getJSONObject("Result").getJSONArray("Sentences").size());
                for (int i = 0; i < jsonResult.getJSONObject("Result").getJSONArray("Sentences").size(); i++) {
                    System.out.println("Result.Sentences[" + i + "].BeginTime: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("BeginTime"));
                    System.out.println("Result.Sentences[" + i + "].EndTime: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("EndTime"));
                    System.out.println("Result.Sentences[" + i + "].SilenceDuration: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("SilenceDuration"));
                    System.out.println("Result.Sentences[" + i + "].Text: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("Text"));
                    System.out.println("Result.Sentences[" + i + "].ChannelId: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("ChannelId"));
                    System.out.println("Result.Sentences[" + i + "].SpeechRate: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("SpeechRate"));
                    System.out.println("Result.Sentences[" + i + "].EmotionValue: " +
                        jsonResult.getJSONObject("Result").getJSONArray("Sentences").getJSONObject(i).getString("EmotionValue"));
                }
            }
            else if(matcherClient.matches()) {
                System.out.println("狀態碼以4開頭表示客戶端錯誤......");
            }
            else if(matcherServer.matches()) {
                System.out.println("狀態碼以5開頭表示服務端錯誤......");
            }
            else {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

常見問題

如何結合SDK日志分析延遲問題?

以Java SDK日志為例。

  • 一句話識別的延遲為一句話說完開始,到收到最終識別結果為止,消耗的時間。

    在日志中搜索關鍵字StopRecognition以及RecognitionCompleted,分別找到語音發送完畢時的日志,以及一句話識別結束的日志。記錄的時間差即為SDK端記錄的一句話延時,如下日志延遲為:984-844=140(ms)。

    14:24:44.844 DEBUG [           main] [c.a.n.c.transport.netty4.NettyConnection] thread:1,send:{"header":{"namespace":"SpeechRecognizer","name":"StopRecognition","message_id":"bccac69b505f4e2897d12940e5b38953","appkey":"FWpPCaVYDRp6J1rO","task_id":"8c5c28d9a40c4a229a5345c09bc9c968"}}
    14:24:44.984 DEBUG [ntLoopGroup-2-1] [  c.a.n.c.p.asr.SpeechRecognizerListener] on message:{"header":{"namespace":"SpeechRecognizer","name":"RecognitionCompleted","status":20000000,"message_id":"2869e93427b9429190206123b7a3d397","task_id":"8c5c28d9a40c4a229a5345c09bc9c968","status_text":"Gateway:SUCCESS:Success."},"payload":{"result":"北京的天氣。","duration":2959}}
  • 語音合成關注首包延遲,即從發送合成請求開始,到收到第一個語音包為止,消耗的時間。

    日志中搜索關鍵字send,找到這條日志和緊隨其后的一條收到語音包的日志。記錄的時間差即為SDK端記錄的首包延時。如下日志延時為1035-813=222(ms)。

    14:32:13.813 DEBUG [           main] [c.a.n.c.transport.netty4.NettyConnection] thread:1,send:{"payload":{"volume":50,"voice":"Ruoxi","sample_rate":8000,"format":"wav","text":"國家是由領土、人民、文化和政府四個要素組成的,國家也是政治地理學名詞。從廣義的角度,國家是指擁有共同的語言、文化、血統、領土、政府或者歷史等的社會群體。從狹義的角度,國家是一定范圍內的人群所形成的共同體形式。"},"context":{"sdk":{"name":"nls-sdk-java","version":"2.1.0"},"network":{"upgrade_cost":160,"connect_cost":212}},"header":{"namespace":"SpeechSynthesizer","name":"StartSynthesis","message_id":"6bf2a84444434c0299974d8242380d6c","appkey":"FWpPCaVYDRp6J1rO","task_id":"affa5c90986e4378907fbf49eddd283a"}}
    14:32:14.035  INFO [ntLoopGroup-2-1] [  c.a.n.c.protocol.tts.SpeechSynthesizer] write array:6896
  • 實時語音識別SDK日志類似一句話識別,可以從日志中計算語音末尾處延遲(關鍵字:StopTranscriptionTranscriptionCompleted)。

  • RESTful形式訪問,客戶端自帶日志中沒有體現延遲。需要用戶自己編寫代碼,或者查看服務端日志。

Java SDK找不到com.alibaba的JAR包,如何安裝Alibaba Cloud SDK for Java?

請參見V1.0 Java SDK(不推薦)安裝Alibaba Cloud SDK for Java。

Java SDK通過傳入阿里云賬號的AccessKey ID和AccessKey Secret,調用阿里云Java SDK得到client提示錯誤org.json.JSONArray.iterator()Ljava/util/Iterator如何解決?

請確認依賴包是否完整,查找并添加如下兩個依賴JAR包。

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>