开发者学堂课程【微服务+全栈在线教育实战项目演练(SpringCloud Alibaba+SpringBoot):技术点-阿里云视频点播 SDK(获取视频地址)】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/667/detail/11401
技术点-阿里云视频点播 SDK(获取视频地址)
目录:
一、服务端与客户端
二、获取视频的播放地址
三、获取视频播放凭证
四、上传视频到阿里云视频点播服务
五、方法
一、服务端与客户端
(1)服务端:接口部分(Java 代码)
API(提供固定地址)
ADK(直接调用依赖,较常用)
(2)客户端:调用部分(浏览器,安卓等)
二、获取视频的播放地址(根据视频 id 获取到)
点击视频管理,获取视频地址,在浏览器直接就可以播放视频,但经过加密的地址,在浏览器是不可以直接播放的,因此在在数据中不存储地址,而存储视频id。
代码:
接口参数和返回字段请参考 GetPlaynto.
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
/*获取播放地址函数*/public static GetPlayInfoResponse getPlayInfo(DefaultAcsClientclient) throws Exception {
GetPlayInfoRequest request = ner GetPlayInfoRequest();
request.setVideoId("视频ID");
return client.getAcsResponse(request);
/*以下为调用示例*/
public static void main(String[] argv) {
DefaultAcsClientclient=initVodClient("<您的AccessKeyId>",”<您的AccessKeySecret>");
GetPla yInfoResponse response new GetPlayInfoResponse();
List GetPlayInfoResponse.PlayInfo> playInfolist.response.etPlayInfolist();
//播放地址
for (GetPlayInfoResponse.PlayInfo playInfo:playInfolist)
System.out.print("PlayInfo.PlayURL=+ playInfo.getPlayURL()+"Xn");
//Base信息.
System.out.print("VideoBase.Title."+response.getVideoBase().getTitle()+"Xn");]
catch (Exception e)
System.out.print("ErrorMessage.."+e.getLocalizedMessage())
System.out.print("RequestId."+response.getRequestId()+"In")
三、获取视频播放凭证(根据视频id获取到)
拥有播放凭证既可播放不加密视频也可以播放加密视频,是一个许可证。
代码:
接口参数和返回字段请参考 GetVdeoPlayAuth.import com.aliyuncs.vod.model.v20170321.Get Vi deoPlayAuthRequest;import.com.aliyuncs.vod.model.v20170321.Get Vi deoPlayAuthResponse;
/*获取播放凭证函数*/
public static Get VideoPlayAuthResponse get Vi deoPlayAuth
(DefaultAcsClient client) throws Exce
Get VideoPlayAuthRequest request = ner GetvideoPlayAuthRequest();
request.setVideoId("视频ID");
return client.getAcsResponse(request);
/*以下为调用示例*/
public static void main(String[] argv)
DefaultAcsClientclient-initvodClient("<您的AccessKeyId>","<您的AccessKeySecret>");
GetVideoPlayAuthResponseresponse.new GetVideoPla yAuthResponse();
try
response = getVideoPlayAuth(client);
//播放凭证
System.out.print("PlayAuth-"+response.getPlayAuth()+"In");
//VideoMeta信息
System.out.print("VideoMeta.Title..+response.getVideoMeta().getTitle()+"In");
catch (Exception e)
System.out.print("ErrorMessage ="+e.getLocalizedMessage());
System.out.print("RequestId"+response.getRequestId()+"Xn");
四、上传视频到阿里云视频点播服务
五、方法
(1)在 service 创建子模块 service_vod,引入相关依赖,到01-视频点播微服务的创建笔记进行复制。
依赖代码如下:
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
cartifactId>aliyun-java-sdk-core<fartifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
cartifactId>aliyun-sdk-oss<lartifactId)
</dependency><dependency)
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod<fartifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId)</dependency><dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId)
</dependency>
<dependency>
<groupId>orgjson</groupId>
cartifactId>json</artifactId></dependency>(dependency)
groupId>com.google.code.gson</groupId> cartifactId>gson</artifactId)</dependency>
<dependency>
<groupId>joda-time</groupId>
cartifactId>joda-timec/artifactId></dependency></dependencies)
(2)初始化操作,创建DefaultAcsClient对象
public class InitObject{
publicstaticDefaultAcsClientinitVodClient(String accessKeyld,String accessKeySecret)throws ClientException{
String regionld="cn-shanghai";1点播服务接入区城
DefaultProfile profile=DefaultProfile.getProfile(regionld,accessKeyld
accessKeySecret):
DefaultAcsClientclient=new DefaultAcsClient(profile);
return client:
}
(3)实现根据视频id获取视频播放地址
//根据视频 iD 获取视频播放地址
//创建初始化对象DefaultAcsClient client=InitObject.initVodClient(accessKeyld:"LTAI4FvvVEWiTJ3GNJJqJnk7"accessKeySecret:"9st82dv7EvFk9mTjY01xxbM632fRbG”)
//创建获取视频地址request和responseGetPlayInfoRequestrequest=newGetPlayInfoRequest() GetPlayInfoResponse response=newGetPlayInfoResponse()
//向 request 对象里面设置视频 id
request.setVideoId("474be24d43ad4f76af344b9f4daaabd1”)
//调用初始化对象里面的方法,传递request,获取数据
response=client.getAcsResponse(request);
List<GetPlavInfoResponse.PlayInfo>playInfoList=response.getPlayInfoList()://
播放地址
for(GetPlayInfoResponse.PlayInfo playInfo:playInfoList){
Systemoutprint("PlayInfo.PlayURL="+playInfo.getPlayURL()+"\n”)
//Base信息System.outprint("VideoBase.Title="+response.getVideoBase().getTitle()+"\n”);
运行结果:
"D:Program FilesJavaljdk1.8.0_181bin java.exe'
PlayInfo.PlayURL = htto://videotest.xa-src.com/sv/48bb416a-170a0783597/48bb416a-170a0783597.mp4
VideoBase.Title = 6-What If I Want to Move Faster.mp4
Process finished with exit code 0