阿里云VOD(二)

一、准备工作

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.2</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

package com.atguigu.aliyunvod;

public class VodSdkTest {
    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

/*获取播放地址函数*/
public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {
    GetPlayInfoRequest request = new GetPlayInfoRequest();
    request.setVideoId("视频ID");
    //request.setResultType("Multiple"); //如果视频id是加密视频的id,则需要设置此参数。但只能获取文件无法解密播放
    return client.getAcsResponse(request);
}
@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");
}

2、获取视频播放凭证

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

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

/*获取播放凭证函数*/
public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws Exception {
    GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
    request.setVideoId("视频ID");
    return client.getAcsResponse(request);
}
/*以下为调用示例*/
@Test
public void testGetVideoPlayAuth() {

    DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
    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");
}
上一篇:SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'vod_tag'at r


下一篇:从数据库获取数据转为json格式给程序调用