import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import lombok.extern.slf4j.Slf4j; import java.util.*; /** * 提交语音异步检测任务 demo * * @author yinzuomei@elink-cn.com * @date 2020/12/8 21:51 */ @Slf4j public class VoiceAsyncScanRequestSample extends BaseSample { public static void main(String[] args) throws Exception { //请替换成您自己的AccessKey ID、AccessKey Secret。 IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4G6Fv6uTzQbpsayATHq4", //您自己的AccessKey ID "QevMw1RYCwQUG3RSMPq1J6EAfmSblo");//您自己的AccessKey Secret final IAcsClient client = new DefaultAcsClient(profile); VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); //class different vs common asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。 asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。 asyncScanRequest.setRegionId("cn-shanghai"); asyncScanRequest.setConnectTimeout(3000); asyncScanRequest.setReadTimeout(6000); List> tasks = new ArrayList>(); Map task1 = new LinkedHashMap(); // 请将下面的地址修改为要检测的语音文件的地址。 task1.put("dataId","voice1"); task1.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/6480bd6be9f14a458162218cea84dfa5.aac"); Map task2 = new LinkedHashMap(); task2.put("dataId","voice2"); task2.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/b566d94fd7114ffb9a203e80c22ebb96.aac"); tasks.add(task1); tasks.add(task2); JSONObject data = new JSONObject(); System.out.println("==========Task count:" + tasks.size()); data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks); // 如果是语音流检测,则修改为true。 data.put("live", false); asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); System.out.println("接口入参:"+JSON.toJSONString(data, true)); try { HttpResponse httpResponse = client.doAction(asyncScanRequest); if (httpResponse.isSuccess()) { JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println("接口返参:"+JSON.toJSONString(scrResponse, true)); if (200 == scrResponse.getInteger("code")) { JSONArray taskResults = scrResponse.getJSONArray("data"); for (Object taskResult : taskResults) { Integer code = ((JSONObject) taskResult).getInteger("code"); if (200 == code) { final String taskId = ((JSONObject) taskResult).getString("taskId"); System.out.println("submit async task success, taskId = [" + taskId + "]"); } else { System.out.println("task process fail: " + code); } } } else { System.out.println("detect not success. code: " + scrResponse.getInteger("code")); } } else { System.out.println("response not success. status: " + httpResponse.getStatus()); } } catch (Exception e) { log.error("method exception", e); } } }