产品百科 | 如何基于阿里云CDN加速实现按需试用功能
最编程
2024-03-11 08:48:48
...
package com.ali.vod.test;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
/**
* @author wb-zzb
* @date 2020/6/3
*/
public class VodPreviewTest {
public static void main(String[] args) {
//请根据点播服务接入区域填写,详情参见点播服务接入区域(https://help.aliyun.com/document_detail/98194.html?spm=a2c4g.11186623.6.612.51c6534bqLs9Wd)
String regionId = "cn-shanghai";
String accessKeyId = "<your accessKeyId>";
String accessKeySecret = "<your accessKeySecret>";
String videoId = "595d020bad3*****f37433451720";
DefaultAcsClient client = InitVodClient(regionId, accessKeyId, accessKeySecret);
GetPlayInfoResponse response = null;
try {
response = getPlayInfo(client, videoId);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("response = " + JSONObject.toJSONString(response));
}
/**
* 初始化Client
*
* @param regionId
* @param accessKeyId
* @param accessKeySecret
* @return
* @throws ClientException
*/
public static DefaultAcsClient InitVodClient(String regionId, String accessKeyId, String accessKeySecret) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 获取视频播放地址
*
* @param client
* @param videoId
* @return
* @throws Exception
*/
public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String videoId) throws Exception {
GetPlayInfoRequest request = new GetPlayInfoRequest();
request.setVideoId(videoId);
//设置过期时间,单位秒,不设置,默认3600s
request.setAuthTimeout(3600L);
request.setFormats("mp4");
JSONObject playConfig = new JSONObject();
//视频点播试看时长,单位为秒。最小值1
playConfig.put("PreviewTime", "30");
request.setPlayConfig(playConfig.toJSONString());
return client.getAcsResponse(request);
}
}