本教程详细介绍如何使用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.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-live -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-live</artifactId>
            <version>3.8.0</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.live.model.v20161101.DescribeLiveRecordConfigRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveRecordConfigResponse;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordContentRequest;
import com.aliyuncs.live.model.v20161101.DescribeLiveStreamRecordContentResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * DescribeLiveStreamRecordContent        查询录制内容
 * DescribeLiveRecordConfig                查询域名下所有App录制配置
 */
public class Demo{

    // 您的加速域名
    private static String domainName = "live3.aliyuntest.club";
    // 直播流所属应用名称
    private static String appName = "live3.aliyuntest.club";
    // 直播流名称
    private static String streamName = "live3.aliyuntest.club";

    public static void main(String[] args) {
        Gson gson = new Gson();
        // 初始化公共请求参数
        IAcsClient client = initialization();
        // 查询直播录制配置
        DescribeLiveRecordConfigResponse describeLiveRecordConfigResponse = DescribeLiveRecordConfig(client);
        // 处理业务逻辑
        System.out.println("---------------------查询域名下所有App录制配置-------------------");
        System.out.println(gson.toJson(describeLiveRecordConfigResponse));
        // 查询直播录制内容
        DescribeLiveStreamRecordContentResponse describeLiveStreamRecordContentResponse = describeLiveStreamRecordContent(client);
        // 处理业务逻辑
        System.out.println("---------------------查询录制内容-------------------");
        System.out.println(gson.toJson(describeLiveStreamRecordContentResponse));
    }

    /**
     * DescribeLiveRecordConfig      查询域名下所有App录制配置
     */
    private static DescribeLiveRecordConfigResponse DescribeLiveRecordConfig(IAcsClient client) {
        DescribeLiveRecordConfigRequest request = new DescribeLiveRecordConfigRequest();
        // 您的加速域名
        request.setDomainName(domainName);
        // 直播流所属应用名称
        request.setAppName(appName);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * DescribeLiveStreamRecordContent    查询录制内容
     */
    private static DescribeLiveStreamRecordContentResponse describeLiveStreamRecordContent(IAcsClient client) {
        DescribeLiveStreamRecordContentRequest request = new DescribeLiveStreamRecordContentRequest();
        // 直播流名称
        request.setStreamName(streamName);
        // 直播流所属应用名称
        request.setAppName(appName);
        // 您的加速域名
        request.setDomainName(domainName);
        // 开始时间
        // 格式:UTC时间
        // 示例:2015-12-01T17:36:00Z
        request.setStartTime("2019-09-26T08:00:00Z");
        // 结束时间。与StartTime的间隔时间不能超过4天
        // 格式:UTC时间
        // 示例:2015-12-01T17:36:00Z
        request.setEndTime("2019-09-27T08:00:00Z");
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}                

执行结果

正确的执行结果类似如下:
---------------------查询域名下所有App录制配置-------------------
{
    "requestId": "C9FA702C-1C52-41BF-8712-A021069D8BAE", 
    "pageNum": 1, 
    "pageSize": 10, 
    "order": "asc", 
    "totalNum": 2, 
    "totalPage": 1, 
    "liveAppRecordList": [
        {
            "domainName": "live3.aliyuntest.club", 
            "appName": "AppName", 
            "streamName": "*", 
            "ossEndpoint": "oss-cn-shanghai.aliyuncs.com", 
            "ossBucket": "videolivetest", 
            "createTime": "2017-08-16T05:09:41Z", 
            "onDemond": 0, 
            "recordFormatList": [
                {
                    "format": "m3u8", 
                    "ossObjectPrefix": "record/2017-08-15/record/{Date}/AppName/{StreamName}/{StartTime}_{EndTime}", 
                    "sliceOssObjectPrefix": "record/2017-08-15/record/{Date}/AppName/{StreamName}/{UnixTimestamp}_{Sequence}", 
                    "cycleDuration": 900
                }
            ]
        }, 
        {...},
        {...}
    ]
}
---------------------查询录制内容-------------------
{
    "LiveAppRecordList": {
        "LiveAppRecord": [
            {
                "CreateTime": "2016-05-20T09:33:38Z", 
                "DomainName": "xxxxx", 
                "OssEndpoint": "oss-cn-hangzhou.aliyuncs.com", 
                "FormatList": {
                    "Format": [
                        {
                            "Name": "m3u8", 
                            "SliceOssObjectPrefix": "xxx", 
                            "OssObjectPrefix": "xxx"
                        }
                    ]
                }, 
                "AppName": "aliyuntest", 
                "OssBucket": "chimang.bucket"
            }
        ]
    }, 
    "RequestId": "5056369B-D337-499E-B8B7-B761BD37B08A"
}