Java实现阿里云视频点播获取视频地址和标题和凭证小案例
参考官方文档:https://help.aliyun.com/document_detail/61064.html?spm=a2c4g.11186623.6.910.e4973bd9ph1UUk
引入依赖
首先需要依然阿里云视频点播需要用的依赖
<!-- aliyun vod -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<aliyun-java-sdk-core.version>4.5.3</aliyun-java-sdk-core.version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<aliyun-java-sdk-vod>2.15.10</aliyun-java-sdk-vod>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<gson.version>2.8.6</gson.version>
</dependency>
创建初始化类
InitObject
/**
* <p>
* Description:
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2020-07-13 16:24
* @see com.greatdata.vod.controller
*/
public class InitObject {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws Exception {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
获取地址和标题
/**
* <p>
* Description:
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2020-07-13 16:24
* @see com.greatdata.vod.controller
*/
public class InitObject {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws Exception {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
##获取视频地址和标题
/**
* 功能描述:获取视频地址和标题小测试 <br>
* 〈〉
* @Param: []
* @Return: {@link String}
*/
@RequestMapping("aliVodTest01")
public String AliVodTest01() throws Exception {
//初始化,传入key和密钥
DefaultAcsClient client = InitObject.initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
//1.根据视频ID获取视频播放地址
GetPlayInfoRequest request = new GetPlayInfoRequest();
//2.创建获取视频地址 request和response
GetPlayInfoResponse response = new GetPlayInfoResponse();
//3.向request对象里面设置id
request.setVideoId("62b63724de52435bbe462f39ac99236b");
//4.调用初始化对象里面的方法,传递 request,获取数据
try {
response = client.getAcsResponse(request);
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//播放地址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.println("PlayInfo.playURL="+playInfo.getPlayURL()+"\n");
}
} catch (ClientException e) {
e.printStackTrace();
}
//Base信息
System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
return "success";
}
获取视频凭证
/**
* 功能描述: 获取视频凭证<br>
* 〈〉
* @Param: []
* @Return: {@link String}
*/
@RequestMapping("aliVodTest02")
public String AliVodTest02() throws Exception {
//初始化,传入key和密钥
DefaultAcsClient client = InitObject.initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
//根据视频ID获取视频地址
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//像request对象里面设置id
request.setVideoId("62b63724de52435bbe462f39ac99236b");
response = client.getAcsResponse(request);
//播放凭证
System.out.print("播放凭证 PlayAuth = " + response.getPlayAuth() + "\n");
//VideoMeta信息
System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
return "sccuess";
}