由于公司的项目跟保利威合作,公司的视频会上传到保利威云点播平台然后在从保利威平台获取视频播放;这里是上传和播放的 vue 实现;只是简单的实现,具体的操作还需要根据自己的需求进行更改;
1、准备工作
1、注册保利威账户:https://www.polyv.net/ 2、进入保利威的后台管理 选择云点播功能 3、进入api接口选项 并且会拿到你专属的 【userid】【writeToken】【secretkey】 这里的 userid、writetoken、readtoken、secrekey 都是代码中必须的配置项,后面代码实现的时候会用到。
2、上传视频
上传视频 sdk :https://help.polyv.net/index.html#/vod/upload_js/web_upload_plugin
代码实现:
<template>
<div>
<input
type="file"
ref="files"
multiple="multiple"
@change="handleUpload"
>
</div>
</template>
<script>
import PlvVideoUpload from '@polyv/vod-upload-js-sdk'
import md5 from 'js-md5'
export default {
data(){
return {
fileSetting: {
desc: '测试视频上传',
cataid: 1,
tag: 'tag',
luping: 0,
keepsource: 1
}
}
},
methods:{
getToken(videoUpload) {
const ptime = Date.now()
const userid = '8588843127'
const secretkey = 'TNM0R9GKB9'
const writeToken = '42beeed6-f28e-44f2-af64-331e7bc5730a'
const hash = md5(ptime + writeToken)
const sign = md5(secretkey + ptime)
videoUpload.updateUserData({ ptime, hash, sign, userid })
},
autoUpdateUserData(timer, videoUpload) {
this.getToken(videoUpload)
if (timer) {
clearTimeout(timer)
timer = null
}
timer = setTimeout(() => {
this.autoUpdateUserData(timer, videoUpload)
}, 3 * 50 * 1000)
},
uploadFile(files, fileSetting, callback){
let videoUpload = new PlvVideoUpload();
this.autoUpdateUserData(null, videoUpload);
Array.from(files).forEach((file, index) => {
const uploader = videoUpload.addFile(file, {
FileProgress: ({ progress }) => {
const progressSize = (progress * 100).toFixed(2)
callback(index, progressSize)
},
FileSucceed: ({ fileData }) => {
callback(index, fileData)
},
onFileFailed: ({ errData }) => {
callback(index, errData)
}
}, fileSetting)
videoUpload.startAll();
})
},
handleUpload(event){
if(!event.target.value){
this.$message.error('请选择需要上传的文件!');
return false;
}
this.uploadFile(event.target.files, this.fileSetting, (index, event) => {
console.log(index, event);
})
}
}
}
</script>
上传成功之后会有大约 10 分钟的审核时间,这段时间里视频是不能查看的;审核通过之后就可以正常查看了;
3、播放视频
播放 sdk:https://help.polyv.net/index.html#/vod/js/
代码实现:
<template>
<div>
<div id="player"></div>
</div>
</template>
<script>
export default {
data(){
return {
vodPlayerJs: 'https://player.polyv.net/script/player.js',
vid: '8588843127d9b9388660dbda2acbe59b_8'
}
},
mounted() {
this.loadPlayerScript(this.loadPlayer)
},
destroyed() {
if (this.player) {
this.player.destroy()
}
},
methods:{
loadPlayerScript(callback) {
if (!window.polyvPlayer) {
const myScript = document.createElement('script')
myScript.setAttribute('src', this.vodPlayerJs)
myScript.onload = callback
document.body.appendChild(myScript)
} else {
callback()
}
},
loadPlayer() {
const polyvPlayer = window.polyvPlayer
this.player = polyvPlayer({
wrap: '#player',
width: 800,
height: 533,
vid: this.vid
})
}
}
}
</script>
这个是简单的播放实现,更多的功能可以去播放 sdk 了解更多的配置;
|