1,安装
点击进入官网了解更多:西瓜播放器官网
# 最新稳定版
$ npm install xgplayer
对于已有项目也可以通过 CDN 引入,代码如下:
methods: {
// =================================1,配置一些参数=================================
initPlayer() {
if (!this.url) return console.warn('url is not esist');
const config = {
id: 'video-player',
url: this.url,
fluid: true,
/**倍速播放 */
playbackRate: [0.5, 0.75, 1, 1.5, 2],
defaultPlaybackRate: 1,
playsinline: this.isAppleDevice(), // IOS设备设置,防止被浏览器劫持
'x5-video-player-type': 'h5', // 微信内置浏览器设置,防止被浏览器劫持
'x5-video-orientation': 'portraint',
/**画中画 */
pip: true,
pipConfig: {
bottom: 100,
right: 100,
width: 320,
height: 180,
},
// download: true,
/**初始化首帧 */
videoInit: true,
autoplay: true,
};
//=============================2, 初始化播放器===================================
const player = new Player(config);
if (player) {
this.player = player;
// 这里注册监听 播放和暂停事件
this.player.on('play', () => {
// 视频开始播放
this.$emit('triggerEvent', this.playerEvent);
});
this.player.on('pause', () => {
// 视频已经暂停
this.$emit('triggerEvent', this.playerEvent);
});
this.player.on('exitFullscreen', () => {
window.scrollTo(0, 0);
console.log('已经退出全屏了');
});
}
},
// IOS设备设置,防止被浏览器劫持
isAppleDevice() {
const ua = navigator.userAgent.toLowerCase();
return /iphone|ipad|phone|Mac/i.test(ua);
},
},
5,监听父组件传进来的url 播放路径 然后设置路径
// 监听播放路径的变化
watch: {
url: {
handler(newValue, oldValue) {
if (!this.player) {
this.initPlayer();
return;
}
this.player.src = this.url;
},
},
},
完整代码如下:
1,父组件
<template>
<div>
<h1>西瓜播放器</h1>
<hr />
<div class="videoPlayer">
<xg-player :url="videoUrl"></xg-player>
</div>
</div>
</template>
<script>
import config from '../../config';
import XgPlayer from '@/components/XgPlayer';
export default {
directives: {},
components: {
XgPlayer,
},
data() {
return {
videoUrl: `${config.publicPath}video/30周年完整+音乐音效3.mp4`,
};
}
};
</script>
<style lang="scss" scoped>
.videoPlayer {
height: 300px;
width: 500px;
margin: 0 auto;
}
</style>
2,子组件 - 非常重要(xgplayer)
<template>
<div id="video-player" class="video-player"></div>
</template>
<script>
import Player from 'xgplayer';
export default {
props: {
url: {
// 父组件传过来的视频链接
type: String,
default: '',
},
},
data() {
return {
player: null, //实例
};
},
mounted() {
console.log('传过来的url:', this.url);
// 初始化播放器
this.initPlayer();
},
created() {},
// 监听播放路径的变化
watch: {
url: {
handler(newValue, oldValue) {
if (!this.player) {
this.initPlayer();
return;
}
this.player.src = this.url;
},
},
},
methods: {
// =========================1,设置播放器必要参数===================
initPlayer() {
if (!this.url) return console.warn('url is not esist');
const config = {
id: 'video-player',
url: this.url,
fluid: true,
/**倍速播放 */
playbackRate: [0.5, 0.75, 1, 1.5, 2],
defaultPlaybackRate: 1,
playsinline: this.isAppleDevice(), // IOS设备设置,防止被浏览器劫持
'x5-video-player-type': 'h5', // 微信内置浏览器设置,防止被浏览器劫持
'x5-video-orientation': 'portraint',
/**画中画 */
pip: true,
pipConfig: {
bottom: 100,
right: 100,
width: 320,
height: 180,
},
// download: true,
/**初始化首帧 */
videoInit: true,
autoplay: true,
};
//========================== 2,开始实例化======================
const player = new Player(config);
if (player) {
this.player = player;
// 这里注册监听
// 视频开始播放
this.player.on('play', () => {
this.$emit('triggerEvent', this.playerEvent);
});
// 视频已经暂停
this.player.on('pause', () => {
this.$emit('triggerEvent', this.playerEvent);
});
// 视频退出全屏
this.player.on('exitFullscreen', () => {
window.scrollTo(0, 0);
console.log('已经退出全屏了');
});
}
},
// IOS设备设置,防止被浏览器劫持
isAppleDevice() {
const ua = navigator.userAgent.toLowerCase();
return /iphone|ipad|phone|Mac/i.test(ua);
},
},
};
</script>
<style></style>