阿里云视频点播服务端API和SDK测试

旧街凉风 提交于 2020-12-13 10:24:48

一 准备工作

1 设置不转码

测试之前设置默认“不转码”,以节省开发成本。

2 找到子账户的AccessKey ID

3 给子账户添加授权

AliyunVODFullAccess

4 阅读文档

服务端API

API调用示例参考:https://help.aliyun.com/document_detail/44435.html?spm=a2c4g.11186623.6.708.2c643d44SY21Hb

服务端SDK

SDK将API进行了进一步的封装,使用起来更简单方便。

二 创建和初始化项目

1 创建maven项目

Group:com.atguigu

Artifact:aliyun_vod

2 添加Maven依赖

<dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-vod</artifactId>
        <version>2.15.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

3 创建测试类并初始化

参考文档:https://help.aliyun.com/document_detail/61062.html

/**
* client初始化
*
* @param accessKeyId
* @param accessKeySecret
* @return DefaultAcsClient 客户端
*/
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
    String regionId = "cn-shanghai";  // 点播服务接入区域
    DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}

三 创建测试用例

1 获取视频播放地址

参考文档:https://help.aliyun.com/document_detail/61064.html

/**
* 获取播放地址
*/
@Test
public void testGetPlayInfo() {
    // 初始化客户端
    DefaultAcsClient client = initVodClient(
            "<您的AccessKeyId>",
            "<您的AccessKeySecret>");
    // 创建请求对象
    GetPlayInfoResponse response = new GetPlayInfoResponse();
    try {
        // 发送请求得到响应
        response = getPlayInfo(client);
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        // 播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
        }
        // Base信息
        System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

/**
* 功能描述:获取播放地址函数
*
* @author cakin
* @date 2020/12/12
* @param client 客户端
* @return GetPlayInfoResponse 播放信息响应
*/
public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {


    GetPlayInfoRequest request = new GetPlayInfoRequest(); // 创建请求对象
    request.setResultType("Multiple");
    request.setVideoId("43d6225f1e844bdcadf6f042580614a6"); // 设置请求参数
    return client.getAcsResponse(request); // 发送请求得到响应
}

测试结果:

PlayInfo.PlayURL = http://video1.guli.shop/e9e25fa83f61418fb71767d884c41742/a67a1c2045064b13af09ffc3a45aea31-fd9af78a2f43cf04e6dbf40def56bc63-fd-encrypt-stream.m3u8
PlayInfo.PlayURL = http://video1.guli.shop/e9e25fa83f61418fb71767d884c41742/a67a1c2045064b13af09ffc3a45aea31-865fef9e41d2591d320940ae30a2bccc-ld-encrypt-stream.m3u8
VideoBase.Title = 第八章
RequestId = A83AA626-BCBB-4A11-8C4B-CBA8D376E102

2 获取视频播放凭证

参考文档:https://help.aliyun.com/document_detail/61064.html#h2--div-id-getvideoplayauth-div-2

加密视频必须使用此方式播放

/**
* 获取播放凭证
*/
@Test
public void testGetVideoPlayAuth() {
    DefaultAcsClient client = initVodClient(
            "LTAI4G5BTyMGDvWFjcziEGPU",
            "XlKSpUcIILsdjYWuZXrRRwVbX4XlQ5");
    GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
    try {
        response = getVideoPlayAuth(client);
        //播放凭证
        System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
        //VideoMeta信息
        System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

/**
* 功能描述:获取播放凭证函数
*
* @param client 客户端
* @return GetVideoPlayAuthResponse 视频播放授权信息
* @author cakin
* @date 2020/12/12
* @description:
*/
public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws Exception {
    GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); // 创建请求对象
    request.setVideoId("e9e25fa83f61418fb71767d884c41742"); // 设置请求参数
    return client.getAcsResponse(request); // 发送请求得到响应
}

测试结果

PlayAuth = eyJTZWN1cml0eV54va2VuIjoiQ0FJU2h3TjFxNkZ0NUIyeWZTaklyNWJ6Yzg3TWlleDc5Wml1Ung2RDBuUWJRdXA3blovdXFqejJJSDlJZEhWb0FPOGZ2dlUwbTJ0WTdQc1psck1xRnNVY0d4eVZNWklvdjhVR3JWci9KcExGc3QySjZyOEpqc1ViOTlCTGhsaXBzdlhKYXNEVkVmbDJFNVhFTWlJUi8wMGU2TC8rY2lyWXBUWEhWYlNDbFo5Z2FQa09Rd0M4ZGtBb0xkeEtKd3hrMnQxNFVtWFdPYVNDUHdMU2htUEJMVXhtdldnR2wyUnp1NHV5M3ZPZDVoZlpwMXI4eE80YXhlTDBQb1AyVjgxbExacGxlc3FwM0k0U2M3YmFnaFpVNGdscjhxbHg3c3BCNVN5Vmt0eVdHVWhKL3phTElvaXQ3TnBqZmlCMGVvUUFQb3BGcC9YNmp2QWF3UExVbTliWXhncGhCOFIrWGo3RFpZYXV4N0d6ZW9XVE84MCthS3p3TmxuVXo5bUxMZU9WaVE0L1ptOEJQdzQ0RUxoSWFGMElVRTF5R21DQ2QvWDRvZ3VSUDF6N0VwTG9pdjltamNCSHFIeno1c2VQS2xTMVJMR1U3RDBWSUpkVWJUbHphRUpHZ1RTNExmWldJbGNUS0FNOVd1MlBNYXgzYlFGRHI1M3ZzVGJiWHpa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yYXRpb24iOjI0LjA1NX0sIkFjY2Vzc0tleUlkIjoiU1RTLk5VRjh0eGQzWkJaRUU4MjZ0Tk5lV3JaRUgiLCJQbGF5RG9tYWluIjoidmlkZW8xLmd1bGkuc2hvcCIsIkFjY2Vzc0tleVNlY3JldCI6IkgzYURrMm1zOENrdjY1MzJDWGlabWpVdnYxQ3dMd0VhdWU5bWJHRmk2bjl5IiwiUmVnaW9uIjoiY24tc2hhbmdoYWkiLCJDdXN0b21lcklkIjoxNTIxNjg5NDEwOTk5NDE2fQ==
VideoMeta.Title = 第八章
RequestId = 7408DCE4-FC97-4C24-87EF-ACD97E50273B

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!