引入SDK npm install vod-js-sdk-v6 --save
TencentUpload.vue文件
<template>
<div>
<div>
<el-upload
ref="upload"
action=""
:show-file-list="false"
:http-request="uploadTencentVideo"
:on-change="handleTencentChange"
accept=".mp4,.webm,.wmv,.wm,.asf,.asx,.rm,.rmvb,.ra,.ram,.mpg,.mpeg,.mpe,.vob,.dat,.mov,.3gp,.mp4v,.m4v,.mkv,.avi,.flv,.f4v"
>
上传视频
</el-upload>
<div v-if="showProgress">
<p>{{ videoName }}</p>
<el-progress :percentage="progress"></el-progress>
</div>
</div>
<tencent-player :options="curCatelogItem"></tencent-player>
</div>
</template>
<script>
import TcVod from "vod-js-sdk-v6";
import TencentPlayer from "@/view/tencentPlayer";
export default {
components: { TencentPlayer },
data() {
return {
fileTencentList: [],
showProgress: false,
videoName: "",
progress: "",
curCatelogItem: {},
}
},
methods:{
uploadTencentVideo(params) {
if (this.fileTencentList.length == 0) {
this.showMsg("warning", "请上传视频");
} else {
const getSignature = async () => {
const data = await this.getVodSignature();
return data;
};
const tcVod = new TcVod({
getSignature: getSignature,
});
const mediaFile = this.fileTencentList[0].raw;
let isVideoUploadSuccess = false;
const uploader = tcVod.upload({
mediaFile: mediaFile,
});
uploader.on("media_progress", (info) => {
this.progress = parseInt(info.percent * 100);
});
uploader.on("media_upload", (info) => {
isVideoUploadSuccess = true;
});
uploader.done().then((res) => {
if (isVideoUploadSuccess === true) {
this.curCatelogItem.fileId = res.fileId;
} else {
this.showMsg(
"warning",
"上传腾讯云出错,请联系管理员!"
);
}
});
}
},
handleTencentChange(file, fileList) {
this.fileTencentList = fileList;
if (this.fileTencentList.length > 1) {
this.fileTencentList.splice(0, 1);
this.videoName = "";
this.progress = 0;
}
this.showProgress = true;
this.videoName = this.fileTencentList[0].raw.name;
},
getVodSignature() {
let that = this;
const url = "/api/File/GetVodSign";
return axios.post(url).then((res) => {
if (res.Code == 200) {
return res.Data.signStr;
}
});
},
},
}
</script>
- 腾讯云SDK规定getSignature签名必须是一个函数
- 全局的axios请求头不能有自定义添加的参数,否则会报跨域错
TencentPlayer.vue文件
<template>
<div>
<video :id="tcPlayerId" class="tencent-player" preload="auto" playsinline webkit-playsinline></video>
</div>
</template>
<script>
function loadTcScript(cb) {
loadScript(cb, {
id: 'tcPlayerScriptId',
url: '//imgcache.qq.com/open/qcloud/video/tcplayer/tcplayer.min.js',
});
}
function loadScript(cb, obj) {
if (document.getElementById(obj.id)) {
cb();
} else {
const script = document.createElement('script');
script.async = true;
script.src = obj.url;
script.id = obj.id;
script.onload = () => {
cb();
};
document.body.appendChild(script);
}
}
export default {
name: 'TencentPlayer',
props: {
options: {
type: Object,
default() {
return {};
}
}
},
data() {
return {
tcPlayerId : 'tcPlayer' + Date.now(),
player: null,
};
},
watch: {
options: {
handler(newV, oldV) {
this.$nextTick(() => {
this.loadJS();
});
},
immediate: true,
deep: true,
}
},
beforeDestroy () {
this.player.dispose()
},
methods: {
loadJS() {
if (window.TCPlayer) {
this.initVideo();
} else {
loadTcScript(() => {
this.initVideo();
});
}
},
initVideo() {
const playerParm = {
fileID: this.options.fileId,
appID: '1256163091',
};
setTimeout(() => {
if (!this.player) {
this.player = TCPlayer(this.tcPlayerId, playerParm);
} else {
this.player.loadVideoByID(playerParm);
}
});
}
},
};
</script>
<style scoped>
@import url("//imgcache.qq.com/open/qcloud/video/tcplayer/tcplayer.css");
.tencent-player {
width:650px;
height:440px;
margin: 0 auto;
}
</style>
- 点播播放器两个必传参数fileID和appID
- 报错Error Code:4000,可能是appID不对