Skip to content

Java

获取 SDK

提示

Windows 用户需要安装 VC++ 运行时环境。

点击下载

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)

识别多媒体文件指定位置

参数描述
filePathstring,多媒体文件路径
startSecondsint,识别开始位置(单位:秒)
lenSecondsint,识别长度(单位:秒,默认为 10,最大不超过 12)
userParamsMap<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)

识别已经读取的多媒体文件指定位置

参数描述
fileBufferstring,多媒体文件路径
fileBufferLenint, buffer 的大小
startSecondsint,识别开始位置(单位:秒)
audioLenSecondsint,识别长度(单位:秒,默认为 10,最大不超过 12)
userParamsMap<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
fpBufferLenint, buffer 的大小
startSecondsint,识别开始位置(单位:秒)
audioLenSecondsint,识别长度(单位:秒,默认为 10,最大不超过 12)
userParamsMap<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)

获取多媒体文件的总时长

参数描述
filePathstring,多媒体文件路径

示例代码:

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)

获取指纹文件的总时长

参数描述
dataBufferbyte[],读取的指纹文件的buffer
dataBufferLenint,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 隐私政策: 链接