123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- import { defineComponent, PropType } from 'vue';
- import styles from './index.module.less';
- import Plyr from 'plyr';
- import 'plyr/dist/plyr.css';
- import { Loading } from 'vant';
- import { browser } from '@/helpers/utils';
- export default defineComponent({
- name: 'm-video',
- props: {
- setting: {
- type: Object,
- default: () => ({})
- },
- controls: Boolean,
- height: String,
- src: {
- type: String,
- default: ''
- },
- poster: {
- type: String,
- default: ''
- },
- styleValue: {
- type: Object,
- default: () => ({})
- },
- preload: {
- type: String as PropType<'auto' | 'metadata' | 'none'>,
- default: 'auto'
- },
- currentTime: {
- type: Boolean,
- default: true
- },
- playsinline: {
- type: Boolean,
- default: true
- },
- preLoading: {
- type: Boolean,
- default: true
- }
-
-
-
-
- },
- emits: ['exitfullscreen', 'play', 'ready', 'enterfullscreen'],
- data() {
- return {
- player: null as any,
- loading: true
- };
- },
- mounted() {
- this.loading = this.preLoading;
- this._init();
- },
- methods: {
- _init() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- const controls = [
- 'play-large',
- 'play',
- 'progress',
- 'captions',
- 'fullscreen'
- ];
-
-
-
- if (this.currentTime) {
- controls.push('current-time');
- }
- const params: any = {
- controls: controls,
- ...this.setting,
- invertTime: false
- };
- if (browser().iPhone) {
- params.fullscreen = {
- enabled: true,
- fallback: 'force',
- iosNative: true
- };
- }
- this.player = new Plyr((this as any).$refs.video, params);
-
-
-
-
-
- this.player.elements.container
- ? (this.player.elements.container.style.height = this.height || '210px')
- : null;
- if (this.preload === 'none') {
- this.loading = false;
- }
- this.player.on('loadedmetadata', () => {
- this.loading = false;
- this.domPlayVisibility(false);
- });
- this.player.on('loadeddata', () => {
- this.$emit('ready', this.player);
- });
- this.player.on('play', () => {
- this.$emit('play', this.player);
- });
- this.player.on('enterfullscreen', () => {
-
-
-
-
-
-
-
-
-
-
-
-
-
- this.$emit('enterfullscreen');
- });
- this.player.on('exitfullscreen', () => {
- console.log('exitfullscreen');
-
-
- this.$emit('exitfullscreen');
- });
- },
-
- domPlayVisibility(hide = true) {
- const controls = document.querySelector('.plyr__controls');
- const controls2 = document.querySelector('.plyr__control--overlaid');
- if (hide) {
- controls?.setAttribute('style', 'display:none');
- controls2?.setAttribute('style', 'display:none');
- } else {
- controls?.removeAttribute('style');
- setTimeout(() => {
- controls2?.removeAttribute('style');
- }, 200);
- }
- },
- onStop() {
- this.player.stop();
- },
- onExitScreen() {
- this.player.fullscreen.active && this.player.fullscreen.exit();
- },
- onPlay() {
- this.player?.play();
- }
- },
- unmounted() {
- this.player?.destroy();
- },
- render() {
- return (
- <div class={styles['video-container']}>
- <video
- ref="video"
- class={styles['video']}
- src={this.src}
- playsinline={this.playsinline}
- poster={this.poster}
- preload={this.preload}
- style={{ ...this.styleValue }}></video>
- {}
- {}
- {this.loading && (
- <div
- class={styles.loadingVideo}
- style={{
- height: this.height || '210px'
- }}>
- <Loading
- size={36}
- color="#FF8057"
- vertical
- style={{ height: '100%', justifyContent: 'center' }}>
- 加载中...
- </Loading>
- </div>
- )}
- </div>
- );
- }
- });
|