一、学习阿里云视频点播开发文档
1、SDK方式
(1)引入依赖 (2)初始化对象
java
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
(3)实现具体功能
1)创建初始化对象。 2)创建request、response。 3)向request设置视频id。 4)调用初始化对象的方法实现功能。 5)调用方法返回response对象,获取内容。
二、阿里云视频点播功能测试
1、获取视频播放地址
(1)创建命名为voddemo0211的maven工程
(2)在voddemo0211的pom文件中引入依赖
java
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
(3)创建初始化对象
java
package com.aiguigu.voddemo.utils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
public class AliyunVodSDKUtils {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
(4)具体功能实现
java
@Test
public void getVodUrl() throws Exception{
//1创建初始化对象
DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("你自己的阿里云keyid", "你自己的阿里云keysecret
");
//2创建request、response对象
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//3向request设置视频id
request.setVideoId("1c58da7ffce4406088d1bd7cc2104a7b");
//4调用初始化对象的方法实现功能
response = client.getAcsResponse(request);
//5调用方法返回response对象,获取内容
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.println("playInfo.getPlayURL()="+playInfo.getPlayURL());
}
}
2、获取视频播放凭证
java
@Test
public void getPlayAuth() throws Exception{
//1创建初始化对象
DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI3buexRAagkdy", "A6hpWJbF3Zz6wj3jxuBe40Mwryt1Zz");
//2创建request、response对象
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//3向request设置视频id
request.setVideoId("1c58da7ffce4406088d1bd7cc2104a7b");
//4调用初始化对象的方法实现功能
response = client.getAcsResponse(request);
//5调用方法返回response对象,获取内容
String playAuth = response.getPlayAuth();
System.out.println(playAuth);
}
3、上传视频到阿里云视频点播服务
(1)上传需要导入依赖,有jar没有开源,maven是下载不到相关jar包,把jar包手动安装到中心仓库
注意:以下列举出部分依赖jar包的版本,您可直接在您的项目中添加maven依赖,也可以将VODUploadDemo-java-1.4.12.zip包中的所有jar包引入您的项目中使用。其中,aliyun-java-vod-upload-1.4.12.jar 还未正式开源,请您直接引入jar包至您的项目中使用。
(2)手动安装
1)在源码中找到jar包。
2)cmd进入jar包所在目录,使用下面的命令把jar包安装到中心仓库。
java
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.11.jar
查看结果如下:
(3)在voddemo0211的pom文件中引入依赖
java
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.11</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
(4)测试上传
java
@Test
public void testUploadVideo() throws Exception{
String title="6 - What If I Want to Move Faster";
String fileName ="D:\\test\\200211\\6 - What If I Want to Move Faster.mp4";
UploadVideoRequest request = new UploadVideoRequest("你自己的阿里云KeyId", "你自己的阿里云KeySecret", title, fileName);
/* 可指定分片上传时每个分片的大小,默认为1M字节 */
request.setPartSize(1 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
/* 是否开启断点续传, 默认断点续传功能关闭。当网络不稳定或者程序崩溃时,再次发起相同上传请求,可以继续未完成的上传任务,适用于超时3000秒仍不能上传完成的大文件。
注意: 断点续传开启后,会在上传过程中将上传位置写入本地磁盘文件,影响文件上传速度,请您根据实际情况选择是否开启*/
request.setEnableCheckpoint(false);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
} else {
/* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrorMessage=" + response.getMessage() + "\n");
}
}