vue3使用xgPalyer實現(xiàn)截圖功能的方法詳解
在vue3中,用西瓜視頻播放器插件xgPalyer,實現(xiàn)一個video組件。
本文章主要介紹:
① 如何放開截圖功能
② 如何自定義插件
③ 插件如何掛載vue組件
④ 掛載組件如何同父(祖)組件通訊
xgPlayer 版本: 3.0.12-rc.0
正常應該用release版本,因為最新的release版本(3.0.11)不滿足我的功能需要(開啟截圖功能,但關(guān)閉自動下載),所以用3.0.12-rc.0 版本。
安裝及使用xgPalyer
安裝xgPalyer
安裝西瓜視頻播放器插件pnpm install xgPlayer@3.0.12-rc.0
使用xgPalyer
在頁面中引入依賴
import Player from "xgplayer";
import { Events, Plugin } from "xgplayer";
import "xgplayer/dist/index.min.css";
具體使用
template代碼
<template> <div id="mse" ref="videoPlayer" /> </template>
// 播放器 基礎(chǔ)用法
new Player({
// el: videoPlayer.value,
id: "mse",
lang: "zh",
width: "100%",
height: "100%",
// 默認靜音
volume: 0,
autoplay: false,
videoInit: true,
url: videoUrl || "",
fluid: deviceDetection(),
plugins: [],
//傳入倍速可選數(shù)組
playbackRate: [0.5, 0.75, 1, 1.5, 2]
});放開截圖配置。
并配置,截圖后不自動下載saveImg: false 以及允許跨域訪問videoAttributes: { crossOrigin: "anonymous" }
簡要代碼如下
const basicConfig = {
//...
};
const screenShotConfig = {
// 截圖配置
screenShot: {
saveImg: false, // 禁止截圖后下載圖片
quality: 0.92,
type: "image/png",
format: ".png",
position: Plugin.POSITIONS.CONTROLS_RIGHT
},
videoAttributes: {
crossOrigin: "anonymous"
}
};
new Player(Object.assign(basicConfig, screenShotConfig));
自定義插件,并注入
自定義插件
官方文檔介紹自定義插件 在官方文檔的基礎(chǔ)上,簡單做一點調(diào)整~
給xgPalyer注入插件
注入插件、并給插件傳入?yún)?shù)。
下面是省略的寫法
// 在video組件中,初始化組件
const vedioConfig = { //... } // 配置內(nèi)容省略
const player = new Player(vedioConfig);
const params = { //... }
// 方法二:調(diào)用接口注冊插件,并注入?yún)?shù)
player.value.registerPlugin(screenshotListPlugin, params);
給自定義的插件,注入一些參數(shù)方法
player.value.registerPlugin(screenshotListPlugin, params);
在自定義插件中接收參數(shù)
constructor(args) {
super(args);
this.info = args.config; // 傳給這個插件的參數(shù)在args.config
}
完整的自定義插件代碼如下
import { createApp } from "vue";
import { Plugin } from "xgplayer";
import ScreenshotList from "./screenshotList.vue";
const { POSITIONS } = Plugin;
export default class screenshotListPlugin extends Plugin {
// 插件的名稱,將作為插件實例的唯一key值
static get pluginName() {
return "screenshotListPlugin";
}
static get defaultConfig() {
return {
// 掛載在controls的右側(cè),如果不指定則默認掛載在播放器根節(jié)點上
position: POSITIONS.CONTROLS_RIGHT
};
}
constructor(args) {
super(args);
this.vm = null;
this.onScreenshot = this.onScreenshot.bind(this);
this.player = args.player;
this.videoInfo = args.config; // 暫存?zhèn)鹘o這個插件的參數(shù),后續(xù)傳給vue組件使用
}
beforePlayerInit() {
const screenshotListDom = this.find(".screenshot-plugin");
const tipDom = this.find(".cut-num-tip");
this.vm && this.vm.updatePopInfo(screenshotListDom, this.videoInfo, tipDom);
// TODO 播放器調(diào)用start初始化播放源之前的邏輯
}
afterPlayerInit() {
// TODO 播放器調(diào)用start初始化播放源之后的邏輯
}
// ... 其他插件方法
onScreenshot(val) {
// 找到掛載的組件,觸發(fā)截圖方法
this.vm && this.vm.screenshotHanlde(val);
}
afterCreate() {
const screenshotListDom = this.find(".screenshot-plugin");
/**
* 自定義插件 打開列表
* root.__root__為根節(jié)點Vue模板data值
*/
this.onIconClick = e => {
console.log("class為screenshot-list元素點擊回調(diào)", e);
const listExited = ["block"].includes(screenshotListDom.style.display);
screenshotListDom.style.display = listExited ? "none" : "block";
};
// 對當前插件根節(jié)點內(nèi)部類名為.screenshot-list的元素綁定click事件
this.bind(".screenshot-list", "click", this.onIconClick);
// 對當前插件根節(jié)點綁定click事件
//TODO 插件實例化之后的一些邏輯
// 使用 Vue 組件
const ScreenshotListComponent = createApp(ScreenshotList);
this.vm = ScreenshotListComponent.mount(".screenshot-plugin");
}
destroy() {
// 解綁事件等清理工作
this.unbind(".screenshot-list", "click", this.onIconClick);
// 播放器銷毀的時候一些邏輯
super.destroy();
}
render() {
return `<div>
<button class="screenshot-list" style="position: relative;
right: 6px;
width: 85px;
height: 30px;
line-height: 26px;
font-size: 12px;
top: 5px;
vertical-align: top;
cursor: pointer;
border-radius: 8px;
background: transparent;
color: #fff;
right: 0;
border: 2px solid #fff;
text-align: center;
"
>
查看錄像截圖
<span class="cut-num-tip" style="
position: absolute;
width: 18px;
height: 18px;
line-height: 18px;
top: -12px;
right: -3px;
border-radius: 50%;
display: inline-block;
padding: 0 0px;
font-size: 12px;
text-align: center;
background-color: #FF5722;
color: #fff;
">0</span>
</button>
<div class="screenshot-plugin" style="
display: none;
width: 370px;
height: 250px;
position: absolute;
top: -250px;
right: 0px;
background: #fff;
border-radius: 4px;
"></div>
</div>
`;
}
}自定義插件如何掛載vue組件
純html、css實現(xiàn)的頁面,過于原生態(tài)了。淺淺掛載一個vue組件,實現(xiàn)插件內(nèi)的元素展示。
import { createApp } from "vue";
import ScreenshotList from "./screenshotList.vue";
export default class screenshotListPlugin extends Plugin {
// ...
afterCreate() {
// 在插件實例化后,把vue組件,掛載在實例化的插件元素上
// 使用 Vue 組件
const ScreenshotListComponent = createApp(ScreenshotList);
this.vm = ScreenshotListComponent.mount(".screenshot-plugin");
}
}ps. vue插件內(nèi)用的element-plus框架的組件需要單獨引入使用,且要再次引入對應的css,否則既無樣式,組件也不會生效。 例如
<script setup lang="ts">
import { ElPopconfirm } from "element-plus";
import "element-plus/dist/index.css";
</script>
這篇文章寫得跟裹腳布一樣,又臭又長。有點寫煩了。
掛載組件如何同父(祖)組件通訊
用 provide / inject 給組件通訊,只要是同一個祖先,都可以接收到

① 祖輩組件:暴露注冊方法provide("reloadFun", reloadFun);
② 子輩組件:注入方法 const reloadFun = inject("reloadFun");
剩下的就是,子輩和插件間的方法通訊了~~
介紹一個小方法 videoInfo.value.callReloadPage
async function batchFunname() {
try {
addLoadingMask();
const params = { };
await queryFun(params);
message("成功", { type: "success", duration: 3000 });
onSearch();
// callReloadPage 這個方法流轉(zhuǎn)的時序是
// 1. 祖輩(XXXComponent/index) 組件的暴露方法reloadFun
// 2. video組件中,注入方法reloadFun
// 3. video組件中,通過引入插件時,傳參給自定義插件
// 4. 自定義插件在解構(gòu)時 (constructor) 暫存方法再videoInfo對象中
// 5. 自定義插件,在截圖列表(screenshotList)的組件掛載在dom元素后,把videoInfo傳給 組件(screenshotList)
// 6. 組件(screenshotList)接收到后,傳給hook
// 7. hook中批量存證后,刷新表格頁面,展示最新的存證信息。
// ps. 裹腳布都沒這么長
setTimeout(() => {
videoInfo.value.callReloadPage && videoInfo.value.callReloadPage();
}, 1500);
} catch (err) {
console.error(err);
} finally {
loadingMask.value?.close();
}
}
到此為止了,白白
到此這篇關(guān)于vue3使用xgPalyer實現(xiàn)截圖功能的方法詳解的文章就介紹到這了,更多相關(guān)vue3 xgPalyer截圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.x中useRouter()執(zhí)行后返回值是undefined問題解決
這篇文章主要給大家介紹了關(guān)于vue3.x中useRouter()執(zhí)行后返回值是undefined問題的解決方法,文中通過代碼示例介紹的非常詳細,對大家學習或者使用vue3.x具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
教你使用vue-autofit 一行代碼搞定自適應可視化大屏
這篇文章主要為大家介紹了使用vue-autofit 一行代碼搞定自適應可視化大屏教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的操作代碼
這篇文章主要介紹了vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
vue兩組件間值傳遞 $router.push實現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

