本示例介绍如何使用Alibaba Cloud SDK for Java检测实时语音流或语音文件中的垃圾内容。
前提条件
在使用本教程之前,请确保已完成以下操作:
- 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
- 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)。
<dependencies> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-green</artifactId> <version>3.5.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-oss --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.61</version> </dependency> </dependencies>
- 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
代码示例
- 提交语音异步检测任务:
- 提交语音URL示例代码:
import com.alibaba.fastjson.JSON; 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.exceptions.ServerException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.*; public class VoiceAsyncscanURL { public static void main(String[] args) throws Exception { //请替换成您自己的accessKeyId、accessKeySecret IClientProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 您的可用区ID "<your-access-key-id>", // 您的AccessKey ID "<your-access-key-secret>"); // 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("green.cn-hangzhou.aliyuncs.com"); request.setSysVersion("2018-05-09"); request.setSysUriPattern("/green/voice/asyncscan"); List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); Map<String, Object> task1 = new LinkedHashMap<String, Object>(); // 修改下面的地址(如果是语音文件检测,则修改为语音文件地址) task1.put("url", "https://fdfs.xmcdn.com/group21/M07/E1/9A/wKgJKFhaF1LB6La5ABf44Sux-Uw565.m4a"); task1.put("dataId",UUID.randomUUID().toString()); tasks.add(task1); JSONObject data = new JSONObject(); data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks); //如果是语音流检测,则修改为true data.put("live", false); request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); try { CommonResponse response = client.getCommonResponse(request); JSONObject dataJson = JSON.parseObject(response.getData()); System.out.println(JSON.toJSONString(dataJson, true)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
- 提交语音本地文件示例代码:
import com.alibaba.fastjson.JSON; 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.exceptions.ServerException; import com.aliyuncs.green.extension.uploader.ClientUploader; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.*; public class VoiceAsyncscanFile { public static void main(String[] args) throws Exception { //请替换成您自己的accessKeyId、accessKeySecret IClientProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 您的可用区ID "<your-access-key-id>", // 您的AccessKey ID "<your-access-key-secret>"); // 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("green.cn-hangzhou.aliyuncs.com"); request.setSysVersion("2018-05-09"); request.setSysUriPattern("/green/voice/asyncscan"); List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>(); Map<String, Object> task1 = new LinkedHashMap<String, Object>(); // 修改下面的地址(如果是语音文件检测,则修改为语音文件地址) // 如果是语音文件检测,则type修改为file task1.put("type", "file"); String url = null; ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false); try{ url = uploader.uploadFile("C:/Users/admin/Desktop/test.mp3"); }catch (Exception e){ e.printStackTrace(); } task1.put("url", url); task1.put("dataId",UUID.randomUUID().toString()); tasks.add(task1); JSONObject data = new JSONObject(); data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks); request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); try { CommonResponse response = client.getCommonResponse(request); JSONObject dataJson = JSON.parseObject(response.getData()); System.out.println(JSON.toJSONString(dataJson, true)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
- 提交语音URL示例代码:
- 查询语音异步检测结果:
import com.alibaba.fastjson.JSON; 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.exceptions.ServerException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.ArrayList; public class VoiceResults { public static void main(String[] args) throws Exception { //请替换成您自己的accessKeyId、accessKeySecret IClientProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 您的可用区ID "<your-access-key-id>", // 您的AccessKey ID "<your-access-key-secret>"); // 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); // 构造请求 CommonRequest request = new CommonRequest(); // 设置SDK版本 request.setSysVersion("2018-05-09"); // 请求方式 request.setSysMethod(MethodType.POST); // 请求路径 request.setSysDomain("green.cn-hangzhou.aliyuncs.com"); request.setSysUriPattern("/green/voice/results"); // 请务必设置超时时间 request.setSysConnectTimeout(3000); request.setSysReadTimeout(6000); // 设置任务ID ArrayList<String> taskIds = new ArrayList<String>(); taskIds.add("vc_f_29nGvJAHkRJ5Fz1WQQQXr$-1rGG2H"); taskIds.add("vc_f_6blpzlMPkLq7ulzgQjxRnF-1rGGrP"); request.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON); try { CommonResponse response = client.getCommonResponse(request); JSONObject dataJson = JSON.parseObject(response.getData()); System.out.println(JSON.toJSONString(dataJson, true)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }
执行结果
- 提交语音异步检测正确的返回类似如下:
{ "msg":"OK", "code":200, "data":[ { "msg":"OK", "code":200, "taskId":"vc_f_5bY4p5r4QU66a@$DnhAGS2-1rGGuX", "dataId":"7726d2ec-cee0-4ba3-af52-16f2fe36d322", "url":"http://********.oss-cn-*****" } ], "requestId":"1BE943CA-FD4E-4E74-9158-CC747AA55860" }
- 查询语音异步检测结果正确的返回类似如下:
{ "msg":"OK", "code":200, "data":[ { "msg":"OK", "code":200, "results":[ { "rate":99.91, "suggestion":"pass", "details":[ { "startTime":1, "endTime":5, "label":"normal", "text":"顽皮可爱,就是我,我就是。" }, { "startTime":5, "endTime":9, "label":"normal", "text":"收件人李小圈上学记。" }, { "startTime":14, "endTime":19, "label":"normal", "text":"米小圈就是我8月20日星期三。" }, { "startTime":183, "endTime":188, "label":"normal", "text":"这就是我的老爸一个经常犯各种稀奇古怪错误的家伙。" } ], "label":"normal", "scene":"antispam" } ], "taskId":"vc_f_29nGvJAHkRJ5Fz1WQQQXr$-1rGG2H", "url":"https://fdfs.xmcdn.com/group21/M07/E1/9A/wKgJKFhaF1LB6La5ABf44Sux-Uw565.m4a" }, { "msg":"OK", "code":200, "results":[ { "rate":99.91, "suggestion":"pass", "details":[ { "startTime":0, "endTime":5, "label":"normal", "text":"伤感。" }, { "startTime":5, "endTime":9, "label":"normal", "text":"嗯。" }, { "startTime":9, "endTime":29, "label":"normal", "text":"但是出门真的有一点饭形式。太阳宫们跳老人家给了我欣喜妈咪把我手机小萌还是报到世界的肩膀说,放心,放心,这回有你的语音祝盒子67站。" }, { "startTime":29, "endTime":32, "label":"normal", "text":"但有志气,不出人发脾气是还是有霸气。" } ], "label":"normal", "scene":"antispam" } ], "taskId":"vc_f_6blpzlMPkLq7ulzgQjxRnF-1rGGrP", "url":"http://aligreen-misc-shanghai.oss-cn-shanghai.aliyuncs.com/outer_upload/1231579085529123/videos/-1975620271?Expires=1574825356&OSSAccessKeyId=LTAIFoHlytmLmBNF&Signature=KwacJp45qxn%2FdHIhYZzm2m%2Fe1xw%3D" } ], "requestId":"EF678DC0-2B74-4FEB-A46F-EE3BCC336BB3" }
在文档使用中是否遇到以下问题
更多建议
匿名提交