<script src="../lib/es6-promise.min.js"></script>
<script src="../lib/aliyun-oss-sdk6.17.1.min.js"></script>
<script src="../aliyun-vod-upload-sdk1.5.2.min.js"></script>
也可以模块化引入挂载再Window上
import OSS from '../lib/aliyun-upload-sdk/lib/aliyun-oss-sdk-6.17.1.min'
window.OSS = OSS;
import '../lib/aliyun-upload-sdk/aliyun-upload-sdk-1.5.2.min'
这一步需要服务端的接入客户端向服务端请求接口,根据上床方式向服务端请求请求上传地址和凭证或者请求STS临时Token,用于后续上传的鉴权。
官方比较推荐使用上传地址和凭证我们应当更具实际需求来选择上传方式
使用获取到的上传地址和凭证或STS临时Token作为入参初始化上传实例。
//上传完成的函数
fileChange(e) {
this.file = e.target.files[0];
if (!this.file) {
alert("请先选择需要上传的文件!");
return;
}
var Title = this.file.name;
var userData = '{"Vod":{}}';//只有在STS方式上传时需要在SDK指定,如果是上传地址和凭证方式,则服务费端指定
if (this.uploader) {
this.uploader.stopUpload();//停止上传的方法
this.authProgress = 0;//上传的进度
this.statusText = "";//上传的状态(停止,失败等)
}
this.uploader = this.createUploader();//创建实例后返回创建的实例uploader挂载在this上
this.uploader.addFile(this.file, null, null, null, userData);//将选中的文件添加到上传列表中。
},
//开始上传事件
authUpload() {
// 然后调用 startUpload 方法, 开始上传
if (this.uploader !== null) {
this.uploader.startUpload();
}
},
// 暂停上传
pauseUpload() {
if (this.uploader !== null) {
this.uploader.stopUpload();
}
},
// 恢复上传
resumeUpload() {
if (this.uploader !== null) {
this.uploader.startUpload();
}
},
createUploader(type) {
let self = this;
let uploader = new AliyunUpload.Vod({
//各类默认参数
partSize: self.partSize || 1048576,
parallel: self.parallel || 5,
retryCount: self.retryCount || 3,
retryDuration: self.retryDuration || 2,
region: self.region,
userId: self.userId,
// 添加文件成功
addFileSuccess: function(uploadInfo) {
self.statusText = "添加文件成功, 等待上传...";
console.log("addFileSuccess: " + uploadInfo.file.name);
},
// 开始上传
onUploadstarted: async function(uploadInfo) {
// 如果是 UploadAuth 上传方式, 需要调用 uploader.setUploadAuthAndAddress 方法
// 如果 uploadInfo.videoId 有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
// 如果 uploadInfo.videoId 存在, 调用 刷新视频上传凭证接口
// 如果 uploadInfo.videoId 不存在,调用 获取视频上传地址和凭证接口
if (!uploadInfo.videoId) {
let { Bag } = await api.aliyun_video_on_demandApi({
Title: "上传测试",
FileName: uploadInfo.file.name
});//向服务端获取上传的凭证
let uploadAuth = Bag.UploadAuth;
let uploadAddress = Bag.UploadAddress;
let videoId = Bag.VideoId;
selt.videoId = Bag.VideoId;
uploader.setUploadAuthAndAddress(
uploadInfo,
uploadAuth,
uploadAddress,
videoId
);
self.statusText = "文件开始上传...";
console.log(
"onUploadStarted:" +
uploadInfo.file.name +
", endpoint:" +
uploadInfo.endpoint +
", bucket:" +
uploadInfo.bucket +
", object:" +
uploadInfo.object
);
} else {
// 如果videoId有值,根据videoId刷新上传凭证
// https://help.aliyun.com/document_detail/55408.html?spm=a2c4g.11186623.6.630.BoYYcY
let refreshUrl =
"https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/RefreshUploadVideo?BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&Title=haha1&FileName=xxx.mp4&VideoId=" +uploadInfo.videoId;
axios.get(refreshUrl).then(({ data }) => {
let uploadAuth = data.UploadAuth;
let uploadAddress = data.UploadAddress;
let videoId = data.VideoId;
uploader.setUploadAuthAndAddress(
uploadInfo,
uploadAuth,
uploadAddress,
videoId
);
});
}
},
// 文件上传成功
onUploadSucceed: function(uploadInfo) {
console.log(
"onUploadSucceed: " +
uploadInfo.file.name +
", endpoint:" +
uploadInfo.endpoint +
", bucket:" +
uploadInfo.bucket +
", object:" +
uploadInfo.object
);
console.log(this, "这是this");
self.uploadname = uploadInfo.file.name;
self.statusText = "文件上传成功!";
console.log(this.uploadname);
},
// 文件上传失败
onUploadFailed: function(uploadInfo, code, message) {
console.log(
"onUploadFailed: file:" +
uploadInfo.file.name +
",code:" +
code +
", message:" +
message
);
self.statusText = "文件上传失败!";
},
// 取消文件上传
onUploadCanceled: function(uploadInfo, code, message) {
console.log(
"Canceled file: " +
uploadInfo.file.name +
", code: " +
code +
", message:" +
message
);
self.statusText = "文件已暂停上传";
},
// 文件上传进度,单位:字节, 可以在这个函数中拿到上传进度并显示在页面上
onUploadProgress: function(uploadInfo, totalSize, progress) {
console.log(
"onUploadProgress:file:" +
uploadInfo.file.name +
", fileSize:" +
totalSize +
", percent:" +
Math.ceil(progress * 100) +
"%"
);
let progressPercent = Math.ceil(progress * 100);
self.authProgress = progressPercent;
self.statusText = "文件上传中...";
},
// 上传凭证超时
onUploadTokenExpired: function(uploadInfo) {
// 上传大文件超时, 如果是上传方式一即根据 UploadAuth 上传时
// 需要根据 uploadInfo.videoId 调用刷新视频上传凭证接口重新获取 UploadAuth
// 然后调用 resumeUploadWithAuth 方法, 这里是测试接口, 所以我直接获取了 UploadAuth
self.statusText = "文件超时...";
let refreshUrl ="https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/RefreshUploadVideo?BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&Title=haha1&FileName=xxx.mp4&VideoId=" +//uploadInfo.videoId;
axios.get(refreshUrl).then(({ data }) => {
let uploadAuth = data.UploadAuth;
uploader.resumeUploadWithAuth(uploadAuth);
console.log(
"upload expired and resume upload with uploadauth " + uploadAuth
);
});
},
// 全部文件上传结束
onUploadEnd: function(uploadInfo) {
console.log("onUploadEnd: uploaded all the files");
self.statusText = "文件上传完毕";
}
});
return uploader;
}
上传完成后会返回记得保存videoId,videoId可以获取播放器地址等,是很重要的凭证
获取上传列表
var list = uploader.listFiles();
for (var i=0; i<list.length; i++) {
log("file:" + list[i].file.name);
}
删除上传文件
uploader.deleteFile(index);//需要删除的文件index,对应listFiles接口返回列表中元素的索引
恢复单个上传文件
uploader.resumeFile(index);
清除上传文件列表
uploader.cleanList();
阿里云的视频点播是通过配套的SDK播放器来进行播放我们主要介绍一下其中的web播放器
<head>
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.9.23/skins/default/aliplayer-min.css" /> //(必须)H5模式播放器,需引用此css文件。
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.23/aliplayer-h5-min.js"></script> //(必须)引入H5模式的js文件。
</head>
*3.提供挂载元素
需要指定对应元素挂载播放器
<body>
<div id="J_prismPlayer"></div>
</body>
var player = new Aliplayer({
id: 'J_prismPlayer',
source: '' ,//播放地址,可以是第三方点播地址,或阿里云点播服务中的播放地址。
},function(player){
console.log('The player is created.')
});
阿里云提供的播放器拥有很多功能我们这个介绍几个常用功能,更多需去官网了解
//指定播放时间time为指定的时间,单位:秒。
player.seek(time)
//暂停播放
player.pause()
//旋转设置旋转角度。<角度>正数为顺时针旋转,负数为逆时针旋转。如:player.setRotate(180)表示顺时针旋转180度。
player.setRotate(<角度>)
//获取旋转角度。
player.getRotate()
//镜像
//水平镜像
player.setImage('horizon')
//垂直镜像
player.setImage('vertical')
//设置音量
//volume的值为0~1之间的实数。
player.setVolume(0)
//获取音量信息。
player.getVolume()
//倍数播放设置倍速。以下示例表示设置为2倍速。
player.setSpeed(2)