阿里雲視訊點播服務端API和SDK測試

chengqiuming發表於2020-12-12

一 準備工作

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 = eyJTZWN1cml0eV54va2VuIjoiQ0FJU2h3TjFxNkZ0NUIyeWZTaklyNWJ6Yzg3TWlleDc5Wml1Ung2RDBuUWJRdXA3blovdXFqejJJSDlJZEhWb0FPOGZ2dlUwbTJ0WTdQc1psck1xRnNVY0d4eVZNWklvdjhVR3JWci9KcExGc3QySjZyOEpqc1ViOTlCTGhsaXBzdlhKYXNEVkVmbDJFNVhFTWlJUi8wMGU2TC8rY2lyWXBUWEhWYlNDbFo5Z2FQa09Rd0M4ZGtBb0xkeEtKd3hrMnQxNFVtWFdPYVNDUHdMU2htUEJMVXhtdldnR2wyUnp1NHV5M3ZPZDVoZlpwMXI4eE80YXhlTDBQb1AyVjgxbExacGxlc3FwM0k0U2M3YmFnaFpVNGdscjhxbHg3c3BCNVN5Vmt0eVdHVWhKL3phTElvaXQ3TnBqZmlCMGVvUUFQb3BGcC9YNmp2QWF3UExVbTliWXhncGhCOFIrWGo3RFpZYXV4N0d6ZW9XVE84MCthS3p3TmxuVXo5bUxMZU9WaVE0L1ptOEJQdzQ0RUxoSWFGMElVRTF5R21DQ2QvWDRvZ3VSUDF6N0VwTG9pdjltamNCSHFIeno1c2VQS2xTMVJMR1U3RDBWSUpkVWJUbHphRUpHZ1RTNExmWldJbGNUS0FNOVd1M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