本文为您提供了服务端 Java SDK 的媒资播放模块的接口调用示例。
由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个临时播放 Token,您可以循环调用生成方法。
说明
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 { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); String vid = "your vid"; // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); 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(); } } }
接口请求参数和返回参数说明详见获取播放地址。
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 { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // 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"); reqBuilder.setForceExpire("your ForceExpire"); 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(); } } }
私有加密 Token 用于 Web 点播 SDK 播放火山引擎私有加密视频,详情请见以下文档:
私有加密 Token 可由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个私有加密 Token,您可以循环调用生成方法。
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 { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // 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(); // Web 点播 SDK 内部携带 playAuthId、vid、unionInfo 向应用服务端请求获取私有加密 Token,详细示例代码和参数说明请见 [Web 端播放私有加密视频实现方式部分](https://www.volcengine.com/docs/4/68698#%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F-2) reqBuilder.setVid(vid); // 需指定 DrmType 为 webdevice 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(); } } }
由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个 HlS 标准加密 Token,您可以循环调用生成方法。HlS 标准加密 Token 用于 Web 客户端播放 HLS 加密音视频,详见 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 { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); try { String resp = vodService.createSha1HlsDrmAuthToken(3600L); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }