使用前提与环境准备:服务开通并购买
操作步骤:
1.添加pom依赖
<dependency>
<groupId>com.alibaba.nls</groupId>
<artifactId>nls-sdk-recognizer</artifactId>
<version>2.2.1</version>
</dependency>
<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>
2.Code Sample
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 AudioEventDetectionDemo {
public static final String TOKEN = "XXXXXXXXXX";
public static final String APPKEY = "XXXXXXXXXX";
private static final String NAMESPACE = "AudioEventDetection";
private static final String URL = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1";
private static final int SAMPLE_RATE = 16000;
private static final int CHUNK_DURATION = 200;
public static void main(String[] args) throws Exception {
NlsClient client = new NlsClient(URL, TOKEN);
//InputStream stream = AudioEventDetectionDemo.class.getResourceAsStream("/test.pcm");
FileInputStream stream = new FileInputStream("D:\\download\\test.pcm");
CommonRequestListener listener = getListener();
CommonRequest request = new CommonRequest(client, listener, NAMESPACE);
request.setAppKey(APPKEY);
request.addCustomedParam("format", "pcm");
request.addCustomedParam("sample_rate", 16000);
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;
}
}
3.测试结果
onFailed, taskId: f0d669851a1a40e8804d1e11651b756c, header: {namespace=Default, name=TaskFailed, message_id=80455ce2482748b1b85260a4634f5c93, task_id=f0d669851a1a40e8804d1e11651b756c, status_text=Gateway:FREE_TRIAL_EXPIRED:The free trial has expired!, status=40000010}, payload: {}
- 目前由于语音分析服务并不支持免费试用版,需要控制台开通商用版才可正常试用
4.升级商用后再次测试结果
onEvent, taskId: f06635cda4624969850a314ab99fc6db, header: {namespace=AudioEventDetection, name=TaskResult, message_id=de21af540281426e8c6cd573fb92c15e, task_id=f06635cda4624969850a314ab99fc6db, status_text=Gateway:SUCCESS:Success., status=20000000}, payload: {result=[{"start_time":0,"event_type":"Music","confidence":0.8432333469390869,"end_time":1970}], number=1}
更多参考
开发指南
声音事件检测