使用语音反垃圾Java SDK接口检测实时语音流或语音文件中的垃圾内容。
背景信息
语音流检测和语音文件检测均为异步检测,检测结果需要您以轮询或者回调的方式获取。
语音检测按照检测的语音文件或语音流的时间长度进行计费。时间单位为分钟,以每天累计检测总时长进行计量统计,每天检测总时长不足一分钟的按照一分钟进行计费。
相关API接口文档,请参见语音异步检测。
准备工作
在进行具体的服务调用之前,请完成以下准备工作:
- 创建阿里云AccessKey。关于创建阿里云AccessKey的具体操作,请参见创建AccessKey。
- 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖。
- 可选:如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
提交语音异步检测任务
接口 | 描述 | 支持的地域 |
---|---|---|
VoiceAsyncScanRequest | 异步检测语音流或语音文件中是否包含违规内容。语音流格式支持:
|
cn-shanghai :华东2(上海)
|
示例代码
- 提交语音URL进行检测
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.green.model.v20180509.VoiceAsyncScanResultsRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.*; public class Main { public static void main(String[] args) throws Exception { // 请替换成您自己的AccessKey ID、AccessKey Secret。 IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的AccessKey ID", "您自己的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<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); Map<String, Object> task1 = new LinkedHashMap<String, Object>(); // 请将下面的地址修改为要检测的语音文件的地址。 task1.put("url", "https://xxxxxxxxxxxxxx"); tasks.add(task1); 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) { e.printStackTrace(); } } }
- 提交本地语音文件进行检测
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.green.model.v20180509.VoiceAsyncScanResultsRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.green.extension.uploader.ClientUploader; import java.util.*; public class Main { public static void main(String[] args) throws Exception { // 请替换成您自己的AccessKey ID、AccessKey Secret。 IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的AccessKey ID", "您自己的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<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); Map<String, Object> task1 = new LinkedHashMap<String, Object>(); task1.put("type", "file"); String url = null; ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false); try{ url = uploader.uploadFile("d:/test.mp4"); }catch (Exception e){ e.printStackTrace(); } task1.put("url", url); tasks.add(task1); JSONObject data = new JSONObject(); System.out.println("==========Task count:" + tasks.size()); data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks); 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) { e.printStackTrace(); } } }
查询语音异步检测结果
接口 | 描述 | 支持的地域 |
---|---|---|
VoiceAsyncScanResultsRequest | 查询异步语音检测结果。 | cn-shanghai :华东2(上海)
|
示例代码
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.VoiceAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// 请替换成您自己的AccessKey ID、AccessKey Secret。
IClientProfile profile = DefaultProfile
.getProfile("cn-shanghai", "您自己的AccessKey ID", "您自己的AccessKey Secret");
final IAcsClient client = new DefaultAcsClient(profile);
pollingScanResult(client, "提交语音异步检测任务后返回的taskId");
}
public static void pollingScanResult(IAcsClient client, String taskId) throws InterruptedException {
int failCount = 0;
boolean stop = false;
do {
// 设置每10秒查询一次。
Thread.sleep(10 * 1000);
JSONObject scanResult = getScanResult(client, taskId);
if (scanResult == null || 200 != scanResult.getInteger("code")) {
failCount++;
System.out.println(taskId + ": get result fail, failCount=" + failCount);
if (scanResult != null) {
System.out.println(taskId + ": errorMsg:" + scanResult.getString("msg"));
}
if (failCount > 20) {
break;
}
continue;
}
JSONArray taskResults = scanResult.getJSONArray("data");
if (taskResults.isEmpty()) {
System.out.println("failed");
break;
}
for (Object taskResult : taskResults) {
JSONObject result = (JSONObject) taskResult;
Integer code = result.getInteger("code");
if (280 == code) {
System.out.println(taskId + ": processing status: " + result.getString("msg"));
} else if (200 == code) {
System.out.println(taskId + ": ========== SUCCESS ===========");
System.out.println(JSON.toJSONString(scanResult, true));
System.out.println(taskId + ": ========== SUCCESS ===========");
stop = true;
} else {
System.out.println(taskId + ": ========== FAILED ===========");
System.out.println(JSON.toJSONString(scanResult, true));
System.out.println(taskId + ": ========== FAILED ===========");
stop = true;
}
}
} while (!stop);
}
private static JSONObject getScanResult(IAcsClient client, String taskId) {
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
getResultsRequest.setEncoding("utf-8");
getResultsRequest.setRegionId("cn-shanghai");
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("taskId", taskId);
tasks.add(task1);
/**
* 请务必设置超时时间。
*/
getResultsRequest.setConnectTimeout(3000);
getResultsRequest.setReadTimeout(6000);
try {
getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
HttpResponse httpResponse = client.doAction(getResultsRequest);
if (httpResponse.isSuccess()) {
return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
} else {
System.out.println("response not success. status: " + httpResponse.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
短语音同步检测
示例代码
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.VoiceSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// 请替换成您自己的AccessKey ID、AccessKey Secret。
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "您自己的AccessKey ID", "您自己的AccessKey Secret");
final IAcsClient client = new DefaultAcsClient(profile);
VoiceSyncScanRequest asyncScanRequest = new VoiceSyncScanRequest();
asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
asyncScanRequest.setRegionId("cn-shanghai");
asyncScanRequest.setConnectTimeout(3000);
// 由于同步语音检测比较耗时,因此建议将超时时间设置在15秒以上。
asyncScanRequest.setReadTimeout(15000);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
// 请将下面的地址修改为要检测的语音文件的地址。
task1.put("url", "https://xxxxxxxxxxxxxx");
tasks.add(task1);
JSONObject data = new JSONObject();
System.out.println("==========Task count:" + tasks.size());
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);
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) {
System.out.println("task process success, result:" + JSON.toJSONString(taskResult));
} else {
System.out.println("task process fail: " + JSON.toJSONString(taskResult));
}
}
} else {
System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
}
} else {
System.out.println("response fail:" + new String(httpResponse.getHttpContent(), "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在文档使用中是否遇到以下问题
更多建议
匿名提交