官网:西瓜播放器
1、首先安装下载 XgPlay.js依赖
npm i xgplayer --save
npm i xgplayer-hls.js --save
2、页面引用
import FlvPlayer from "xgplayer-flv.js";
import "xgplayer/dist/index.min.css";
3、建立dom容器
// 提供一个容器
// console.log('complete')
// try {
// this.player.play() // 尝试再次执行播放
// setTimeout(() => { // 500毫秒收检测
// if (!this.player.hasStart && this.player.currentTime ===
// 0) { // hasStart返回false,并且播放时间还是0,那么就可以认为自动播放失效了
//
// }
// }, 500)
// } catch (e) {
// console.log(e);
// }
// });
// 监听视频开始播放 播放传给父组件的是true
this.player.on("play", () => {
this.$emit("triggerEvent", true);
});
// 监听视频已经暂停 暂停传给父组件的是true
this.player.on("pause", () => {
this.$emit("triggerEvent", false);
});
// 视频加载失败,报错误提示
this.player.on("error", () => {
console.log("视频加载错误,请确认视频地址后再连接!");
//this.$message.warning('视频加载错误,请确认视频地址后再连接!');
});
// 监听 视频退出全屏
// this.player.on('exitFullscreen', () => {
// window.scrollTo(0, 0);
// console.log('已经退出全屏了');
// });
//检测画面是否卡死。如果decodedFrames不再发生变化,就销毁掉该实例并进行重新连接。
this.player.on("statistics_info", function (res) {
if (_this.lastDecodedFrame == 0) {
_this.lastDecodedFrame = res.decodedFrames;
return;
}
if (_this.lastDecodedFrame != res.decodedFrames) {
_this.lastDecodedFrame = res.decodedFrames;
_this.sameCount = 0;
} else {
_this.sameCount++;
console.log(
"decodedFrames没有发生变化第",
_this.sameCount,
"次。时间:",
new Date()
);
if (_this.sameCount > 9) {
//decodedFrames10秒不变,则判断为画面卡死,销毁实例进行重连
console.log(
"画面卡死,进行重连————————player:",
_this.player,
".时间",
new Date()
);
//重置变量
_this.lastDecodedFrame = 0;
_this.sameCount = 0;
if (_this.player) {
_this.player.destroy(); //销毁
_this.getData(); //重连
console.log("重连完毕");
}
}
}
});
}
},
5、视频效果
6、在Vue中使用封装组件
class="video-player"
:id="id"
className="video-player1"
style="height: 100% !important; padding-top: 0 !important"
>
//import Player from 'xgplayer';
import FlvPlayer from "xgplayer-flv.js";
import "xgplayer/dist/index.min.css";
// import volume from "xgplayer/dist/controls/volume";
// import playbackRate from "xgplayer/dist/controls/playbackRate";
export default {
props: {
......
},
data() {
return {
player: null, //实例
lastDecodedFrame: 0, //最后一个decodedFrame
sameCount: 0, //decodedFrame不变次数
};
},
mounted() {
console.log("传过来的url:", this.url);
// 初始化播放器
this.initPlayer();
},
created() {},
// 监听播放路径的变化
watch: {
url: {
......
},
},
methods: {
// =========================1,设置播放器必要参数===================
initPlayer() {
let _this = this;
if (!this.url) return console.warn("url is not esist");
......
// 菜单控件
controlPlugins: [],
};
//========================== 2,开始实例化======================
const player = new FlvPlayer(config);
if (player) {
this.player = player;
//检测画面是否卡死。如果decodedFrames不再发生变化,就销毁掉该实例并进行重新连接。
this.player.on("statistics_info", function (res) {
if (_this.lastDecodedFrame == 0) {
_this.lastDecodedFrame = res.decodedFrames;
return;
}
if (_this.lastDecodedFrame != res.decodedFrames) {
_this.lastDecodedFrame = res.decodedFrames;
_this.sameCount = 0;
} else {
_this.sameCount++;
console.log(
"decodedFrames没有发生变化第",
_this.sameCount,
"次。时间:",
new Date()
);
if (_this.sameCount > 9) {
//decodedFrames10秒不变,则判断为画面卡死,销毁实例进行重连
console.log(
"画面卡死,进行重连————————player:",
_this.player,
".时间",
new Date()
);
//重置变量
_this.lastDecodedFrame = 0;
_this.sameCount = 0;
if (_this.player) {
_this.player.destroy(); //销毁
_this.getData(); //重连
console.log("重连完毕");
}
}
}
});
}
},
......
},
};
7、使用XgPlay.js组件
7.1 注册组件
import xgPlayVideo from '@/components/xgPlayVideo.vue'
7.2 使用组件
提供测试地址:
//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4
资源共享:
https://download.csdn.net/download/u012967771/88769868https://download.csdn.net/download/u012967771/88769868
相关文章
本文由 用户 于 2024-04-28 发布在 金钥匙,如有疑问,请联系我们。
本文链接:https://www.51969.com/post/18814251.html
发表评论