视频点播服务
最近公司项目需要用到视频播放及管理,多放选型后准备采用阿里云视频点播服务(公司一直用阿里系)
购买了视频点播存储包和视频点播流量包
首先存储配置
- 配置管理 —> 媒资管理配置 —>存储管理 (配置对应存储区域)
注意:
未配置加速域名,通过控制台或存储地址播放、下载资源会产生存储流出费用,不能使用流量包抵扣。
所以记得使用点播服务前记得配好加速域名(即项目应用对应域名)
步骤
maven私有仓库中登陆admin账号,配置阿里云远程仓库(开源)
-
配置 —> Repository —> create repositories —> maven2(proxy)
-
proxy : 配置对应阿里云远程仓库的url:https://oss.sonatype.org/service/local/staging/deploy/maven2/
-
配置name
已经开源的jar可以通过maven依赖引入,如下:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.11</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.14</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>
其中一个vod-upload包没有开源,因此需要将该jar包存入maven私有云仓库或者直接在本地项目中引入。、
aliyun-sdk-vod-upload-1.4.14.jar
这里选择的是将其从阿里云对应视频点播模块中下载,然后将jar传到本公司的私有maven仓库,然后在对应的项中引入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.14</version>
</dependency>
将下载的压缩包中对应的sample中的示例代码放入到对应项目中,参考实现自己的调用代码;
- 上传视频代码部分:
String fileName = file.getOriginalFilename();
//title:上传之后显示名称
String title = fileName.substring(0, fileName.lastIndexOf("."));
//inputStream:上传文件输入流
InputStream inputStream = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
if (response.isSuccess()) {
videoId = response.getVideoId();
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
videoId = response.getVideoId();
}
log.info("上传视频到阿里云oss:返回videoId->" + videoId);
其中accessKeyId,accessKeySecret为阿里云账号中查询
- 查看视频部分代码:
public GetMezzanineInfoResponse getVideoInfo(String videoId) throws Exception {
GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
- 删除视频代码:
public boolean delByVideoId(String videoId) throws ClientException {
DeleteVideoRequest request = new DeleteVideoRequest();
request.setVideoIds(videoId);
client.getAcsResponse(request);
return true;
}
基础业务代码阿里包里都已经提供了,简单调用即可使用。