Appearance
Java
获取 SDK
Github
访问 https://github.com/acrcloud/acrcloud_sdk_java 根据需求选择合适的版本
Maven
xml
<dependency>
<groupId>com.acrcloud.sdks</groupId>
<artifactId>com.acrcloud.sdks.recognizer</artifactId>
<version>1.0.1</version>
</dependency>
初始化
首先进入 ACRCloud 开发者平台 控制台 > 音频/视频识别 来获取 Access Key, Access Secret 以及 Host。 然后将获取到的配置信息导入 ACRCloudRecognizer 并初始化。
java
import java.util.Map;
import java.util.HashMap;
import com.acrcloud.utils.ACRCloudRecognizer;
import com.acrcloud.utils.ACRCloudExtrTool;
public class Test {
public static void main(String[] args) {
Map<String, Object> config = new HashMap<String, Object>();
config.put("host", "获取到的 Host");
config.put("access_key", "获取到的 Access Key");
config.put("access_secret", "获取到的 Access Secret");
config.put("rec_type", ACRCloudRecognizer.RecognizerType.AUDIO); // AUDIO, HUMMING, BOTH
config.put("debug", false);
config.put("timeout", 10); // seconds
ACRCloudRecognizer re = new ACRCloudRecognizer(config);
}
}
方法
recognizeByFile(String filePath, int startSeconds, int audioLenSeconds, Map<String, String> userParams)
识别多媒体文件指定位置
参数 | 描述 |
---|---|
filePath | string,多媒体文件路径 |
startSeconds | int,识别开始位置(单位:秒) |
lenSeconds | int,识别长度(单位:秒,默认为 10,最大不超过 12) |
userParams | Map<String, String>,搜索参数(仅哼唱搜索有用) |
示例代码:
java
import java.util.Map;
import java.util.HashMap;
import com.acrcloud.utils.ACRCloudRecognizer;
import com.acrcloud.utils.ACRCloudExtrTool;
public class Test {
public static void main(String[] args) {
Map<String, Object> config = new HashMap<String, Object>();
config.put("host", "获取到的 Host");
config.put("access_key", "获取到的 Access Key");
config.put("access_secret", "获取到的 Access Secret");
config.put("rec_type", ACRCloudRecognizer.RecognizerType.AUDIO); // AUDIO, HUMMING, BOTH
config.put("debug", false);
config.put("timeout", 10); // seconds
ACRCloudRecognizer re = new ACRCloudRecognizer(config);
String filename = "test.mp4";
String result = re.recognizeByFile(filename, 0);
System.out.println(result);
}
}
recognizeByFileBuffer(byte[] fileBuffer, int fileBufferLen, int startSeconds, int audioLenSeconds, Map<String, String> userParams)
识别已经读取的多媒体文件指定位置
参数 | 描述 |
---|---|
fileBuffer | string,多媒体文件路径 |
fileBufferLen | int, buffer 的大小 |
startSeconds | int,识别开始位置(单位:秒) |
audioLenSeconds | int,识别长度(单位:秒,默认为 10,最大不超过 12) |
userParams | Map<String, String>,搜索参数(仅哼唱搜索有用) |
示例代码:
java
import java.util.Map;
import java.util.HashMap;
import com.acrcloud.utils.ACRCloudRecognizer;
import com.acrcloud.utils.ACRCloudExtrTool;
import java.io.*;
public class Test {
public static void main(String[] args) {
Map<String, Object> config = new HashMap<String, Object>();
config.put("host", "获取到的 Host");
config.put("access_key", "获取到的 Access Key");
config.put("access_secret", "获取到的 Access Secret");
config.put("rec_type", ACRCloudRecognizer.RecognizerType.AUDIO); // AUDIO, HUMMING, BOTH
config.put("debug", false);
config.put("timeout", 10); // seconds
ACRCloudRecognizer re = new ACRCloudRecognizer(config);
String filename = "test.mp4";
File file = new File(filename);
byte[] buffer = new byte[(int) file.length()];
if (!file.exists()) {
return;
}
FileInputStream fin = null;
int bufferLen = 0;
try {
fin = new FileInputStream(file);
bufferLen = fin.read(buffer, 0, buffer.length);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("bufferLen=" + bufferLen);
if (bufferLen <= 0)
return;
String result = re.recognizeByFileBuffer(buffer, bufferLen, 0);
System.out.println(result);
}
}
recognizeByFpBuffer(byte[] fpBuffer, int fpBufferLen, int startSeconds, int audioLenSeconds)
识别已经读取的指纹文件指定位置
生成指纹文件的方法和详情见 通用工具 > 提指纹工具 部分
参数 | 描述 |
---|---|
fpBuffer | []byte,读取的指纹文件的 buffer |
fpBufferLen | int, buffer 的大小 |
startSeconds | int,识别开始位置(单位:秒) |
audioLenSeconds | int,识别长度(单位:秒,默认为 10,最大不超过 12) |
userParams | Map<String, String>,搜索参数(仅哼唱搜索有用) |
示例代码:
java
import java.util.Map;
import java.util.HashMap;
import com.acrcloud.utils.ACRCloudRecognizer;
import com.acrcloud.utils.ACRCloudExtrTool;
import java.io.*;
public class Test {
public static void main(String[] args) {
Map<String, Object> config = new HashMap<String, Object>();
config.put("host", "获取到的 Host");
config.put("access_key", "获取到的 Access Key");
config.put("access_secret", "获取到的 Access Secret");
config.put("rec_type", ACRCloudRecognizer.RecognizerType.AUDIO); // AUDIO, HUMMING, BOTH
config.put("debug", false);
config.put("timeout", 10); // seconds
ACRCloudRecognizer re = new ACRCloudRecognizer(config);
String filename = "test.mp4.db.lo";
File file = new File(filename);
byte[] buffer = new byte[(int) file.length()];
if (!file.exists()) {
return;
}
FileInputStream fin = null;
int bufferLen = 0;
try {
fin = new FileInputStream(file);
bufferLen = fin.read(buffer, 0, buffer.length);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("bufferLen=" + bufferLen);
if (bufferLen <= 0)
return;
String result = re.recognizeByFpBuffer(buffer, bufferLen, 0);
System.out.println(result);
}
}
ACRCloudExtrTool.getDurationMSByFile(String fileName)
获取多媒体文件的总时长
参数 | 描述 |
---|---|
filePath | string,多媒体文件路径 |
示例代码:
java
import com.acrcloud.utils.ACRCloudExtrTool;
public class Test2 {
public static void main(String[] args) {
String filename = "test.mp4";
int fileDurationMS = ACRCloudExtrTool.getDurationMSByFile(filename);
System.out.println("duration_ms = "+fileDurationMS);
}
}
ACRCloudExtrTool.getDurationMSByFpBuffer(byte[] dataBuffer, int dataBufferLen)
获取指纹文件的总时长
参数 | 描述 |
---|---|
dataBuffer | byte[],读取的指纹文件的buffer |
dataBufferLen | int,buffer 的大小 |
示例代码:
java
import com.acrcloud.utils.ACRCloudExtrTool;
import java.io.*;
public class Test2 {
public static void main(String[] args) {
String filename = "test.mp4.db.lo";
File file = new File(filename);
byte[] buffer = new byte[(int) file.length()];
if (!file.exists()) {
return;
}
FileInputStream fin = null;
int bufferLen = 0;
try {
fin = new FileInputStream(file);
bufferLen = fin.read(buffer, 0, buffer.length);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("bufferLen=" + bufferLen);
if (bufferLen <= 0)
return;
int fileDurationMS = ACRCloudExtrTool.getDurationMSByFpBuffer(buffer, bufferLen);
System.out.println("duration_ms = " + fileDurationMS);
}
}
SDK 合规详情:
SDK名称:ACRCloud 音频识别 SDK
开发者:高第网络技术(北京)有限公司
SDK 合规说明与指引: 链接
SDK 隐私政策: 链接