You need to enable JavaScript to run this app.
导航

媒资播放

最近更新时间2023.10.13 17:01:59

首次发布时间2021.02.23 10:42:40

本文为您提供了服务端 Java SDK 的媒资播放模块相关的 API 调用示例。主要包含:签发 PlayAuthToken、获取播放地址、签发私有 DRM 加密 AuthToken 等。

初始化

使用前请先完成初始化,参考初始化

签发 PlayAuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个PlayAuthToken,您可以循环调用生成方法。PlayAuthToken 用于客户端播放,详见客户端播放

package com.volcengine.example.vod.play;

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodGetPlayInfoRequest;

public class VodGetPlayAuthTokenDemo {

    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();

        String vid = "your vid";

        // call below method if you dont set ak and sk in ~/.vcloud/config
        // vodService.setAccessKey("");
        // vodService.setSecretKey("");

        try {
            VodGetPlayInfoRequest.Builder reqBuilder = VodGetPlayInfoRequest.newBuilder();
            reqBuilder.setVid(vid);

            String resp = vodService.getPlayAuthToken(reqBuilder.build(), 3600L);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

获取播放地址

接口请求参数和返回参数详见 OpenAPI:获取播放地址

package com.volcengine.example.vod.play;

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
public class VodGetPlayInfoDemo {

    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        try {
            com.volcengine.service.vod.model.request.VodGetPlayInfoRequest.Builder reqBuilder = com.volcengine.service.vod.model.request.VodGetPlayInfoRequest.newBuilder();
			reqBuilder.setVid("your Vid");
			reqBuilder.setFormat("your Format");
			reqBuilder.setCodec("your Codec");
			reqBuilder.setDefinition("your Definition");
			reqBuilder.setFileType("your FileType");
			reqBuilder.setLogoType("your LogoType");
			reqBuilder.setBase64("your Base64");
			reqBuilder.setSsl("your Ssl");
			reqBuilder.setNeedThumbs("your NeedThumbs");
			reqBuilder.setNeedBarrageMask("your NeedBarrageMask");
			reqBuilder.setCdnType("your CdnType");
			reqBuilder.setUnionInfo("your UnionInfo");
			reqBuilder.setHDRDefinition("your HDRDefinition");
			reqBuilder.setPlayScene("your PlayScene");
			reqBuilder.setDrmExpireTimestamp("your DrmExpireTimestamp");
			reqBuilder.setQuality("your Quality");
			reqBuilder.setPlayConfig("your PlayConfig");
			reqBuilder.setNeedOriginal("your NeedOriginal");
			
            com.volcengine.service.vod.model.response.VodGetPlayInfoResponse resp = vodService.getPlayInfo(reqBuilder.build());
            if (resp.getResponseMetadata().hasError()) {
                System.out.println(resp.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

签发私有 DRM 加密 AuthToken

PrivateDrmAuthToken 由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 PrivateDrmAuthToken,您可以循环调用生成方法。PrivateDrmAuthToken 用于 Web 客户端播放视频点播自研 DRM 加密音视频,详情可参见客户端播放

说明

在 Web 端播放时,调用 GetPrivateDrmAuthToken 生成方法,需要指定参数 DrmType 取值为 webdevice

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodGetPrivateDrmPlayAuthRequest;

public class VodGetPrivateDrmAuthTokenDemo {
    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();
        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");
        String vid = "your vid";
        String unionInfo = "your unionInfo";
        Long expireTime = 3600L; // token expireTime
        try {
            VodGetPrivateDrmPlayAuthRequest.Builder reqBuilder = VodGetPrivateDrmPlayAuthRequest.newBuilder();
            reqBuilder.setVid(vid);
            reqBuilder.setDrmType("your drm type");
            reqBuilder.setPlayAuthIds("your playAuthIds");
            reqBuilder.setUnionInfo(unionInfo);

            String resp = vodService.getPrivateDrmAuthToken(reqBuilder.build(), expireTime);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

签发 HlS 标准加密 AuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 Sha1HlsDrmAuthToken,您可以循环调用生成方法。Sha1HlsDrmAuthToken 用于 Web 客户端播放 HLS 加密音视频,详见客户端播放

package com.volcengine.example.vod.play;

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;

public class VodGetHlsDecryptionAuthTokenDemo {

    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        try {
            String resp = vodService.createSha1HlsDrmAuthToken(3600L);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}