1. 开通服务
点击管理控制台 选择按照流量计费 无脑下一步即可
2. 点击管理控制台
3. 修改视频上传的配置
4. 存储管理
5. 转码模板组下面加一个
进入开发文档中
6. 查看需要的内容’
API:阿里云提供固定的地址,只需要调用这个固定的地址,向地址传递参数,实现功能(httpClient调用API技术)
SDK:对API进行了封装,使用起来更加方便
7. 准备
引入jar包
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version> 4.4.5</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version> 3.9.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version> 2.15.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version> 1.4.14</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version> 1.2.28</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>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version> 2.10.1</version>
</dependency>
</dependencies>
这里有aliyun-sdk-vod-upload
这个jar包打不上去 因为没有进行公开
需要自己进行手动打入 可以参考下面的教程
8. 初始化操作
//初始化操作
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;
}
注意:cn-shanghai不可修改
9. 获取播放的地址和文件的名字
//获取播放的地址 名字
@Test
public void getPalyUrl()throws Exception{
//1. 根据视频ID获取视频播放地址
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("LTAI5tJzcCqspcFTRYdzatuH", "S3ZDCVB8CoinJV9KHj005c4N4VceVg");
//创建获取视 频地址request和response对象
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//向request对象里设置视频id
request.setVideoId("aed1a27150a94978ae51b15465288731");
//调用初始化对象里面的方法,传递request 获取数据
response = client.getAcsResponse(request);
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//播放地址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
}
//Base信息
System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
}
10. 得到播放的凭证
public static void getPlayAuth() throws ClientException, com.aliyuncs.exceptions.ClientException {
//根据视频id获取视频播放凭证
//创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("LTAI5tJzcCqspcFTRYdzatuH", "S3ZDCVB8CoinJV9KHj005c4N4VceVg");
//获取视频凭证的request和response对象
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//设置视频的id值
request.setVideoId("aed1a27150a94978ae51b15465288731");
//调用初始化对象的方法得到凭证
response = client.getAcsResponse(request);
System.out.println("palyauth:::"+response.getPlayAuth());
}
11. 上传视频的方法
public static void main(String[] args) {
//上传视频的方法
String accessKeyId = "LTAI5tJzcCqspzatuH";//修改为自己的
String accessKeySecret = "S3ZDCVB8Coi5c4N4VceVg";//修改为自己的
String title = "6 - What If I Want to Move Faster - upload by sdk"; //上传之后文件名称
String fileName = "F:/6 - What If I Want to Move Faster.mp4"; //本地文件路径和名称
//上传视频的方法
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
/* 可指定分片上传时每个分片的大小,默认为2M字节 */
request.setPartSize(2 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
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");
}
}
12. 播放视频
- 工具类
@Component
public class ConstantVodUtils implements InitializingBean {
@Value("${aliyun.vod.file.keyid}")
private String keyid;
@Value("${aliyun.vod.file.keysecret}")
private String keysecret;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID=keyid;
ACCESS_KEY_SECRET=keysecret;
}
}
12.1 编写获取视频凭证的接口
//根据视频id获取视频凭证
@GetMapping("getPlayAuth/{id}")
public R getPlayAuth(String id){
try {
//创建初始化对象
DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
//创建获取凭证的request和respond对象
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//向request中设置视频的id
request.setVideoId(id);
GetVideoPlayAuthResponse authResponse = client.getAcsResponse(request);
String playAuth = authResponse.getPlayAuth();
return R.ok().data("playAuth",playAuth);
}catch (Exception e){
new GuliException(20001,"获取视频凭证异常");
return R.ok();
}
}
12.2 前端
点击查看视频按钮
<ol class="lh-menu-ol" style="display: block;">
<li class="lh-menu-second ml30" v-for="video in chapter.children" :key="video.id">
<a :href="'/player/'+video.videoSourceId" target="_blank">
<span class="fr">
<i class="free-icon vam mr10">免费试听</i>
</span>
<em class="lh-menu-i-2 icon16 mr5"> </em>{{video.title}}
</a>
</li>
</ol>
paly文件夹下面的_vid.vue
播放页面
<template>
<div>
<!-- 阿里云视频播放器样式 -->
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.1/skins/default/aliplayer-min.css" >
<!-- 阿里云视频播放器脚本 -->
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.8.1/aliplayer-min.js" />
<!-- 定义播放器dom -->
<div id="J_prismPlayer" class="prism-player" />
</div>
</template>
<script>
import vod from '@/api/vod'
export default {
layout: 'video',//应用video布局
asyncData({ params, error }) {
return vod.getPlayAuth(params.vid)
.then(response => {
return {
playAuth: response.data.data.playAuth,
vid: params.vid
}
})
},
mounted() { //页面渲染之后 created
new Aliplayer({
id: 'J_prismPlayer',
vid: this.vid, // 视频id
playauth: this.playAuth, // 播放凭证
encryptType: '1', // 如果播放加密视频,则需设置encryptType=1,非加密视频无需设置此项
width: '100%',
height: '500px',
// 以下可选设置
cover: 'http://guli.shop/photo/banner/1525939573202.jpg', // 封面
qualitySort: 'asc', // 清晰度排序
mediaType: 'video', // 返回音频还是视频
autoplay: false, // 自动播放
isLive: false, // 直播
rePlay: false, // 循环播放
preload: true,
controlBarVisibility: 'hover', // 控制条的显示方式:鼠标悬停
useH5Prism: true, // 播放器类型:html5
}, function(player) {
console.log('播放器创建成功')
})
}
}
</script>