Vue 2 & 3 简单使用Video.js

什么是Video.js

Video.js是一个有着HTML5背景的网络视频播放器。它同时支持HTM5和Flash视频,简单来说就是HTMl5 和 Flash 视频播放器

vue2

下载依赖

cnpm install video.js

main.js中引入

import Video from 'video.js'
import 'video.js/dist/video-js.css'

Vue.prototype.$video = Video

使用(代码中有注释说明)

    <template>
    <div class="test_two_box">
        <video
        id="myVideo"
        class="video-js"
        >
        <source
            src="//vjs.zencdn.net/v/oceans.mp4"
            type="video/mp4"
        >
        </video>
    </div>
    </template>

    <script>
    /* eslint-disable */
    export default {
    name: "TestTwo",
    data() {
        return {};
    },
    mounted() { 
        this.initVideo();
    },
    methods: {
        initVideo() {
        //初始化视频方法
        let myPlayer = this.$video(myVideo, {
            //确定播放器是否具有用户可以与之交互的控件。没有控件,启动视频播放的唯一方法是使用autoplay属性或通过Player API。
            controls: true,
            //自动播放属性,muted:静音播放
            autoplay: "muted",
            //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
            preload: "auto",
            //设置视频播放器的显示宽度(以像素为单位)
            width: "800px",
            //设置视频播放器的显示高度(以像素为单位)
            height: "400px"
        });
        }
    }
    };
    </script>

    <style scoped>
    </style>

vue3

cnpm install video.js
<template>
  <div class="test_two_box">
    <video ref="videoPlayer" class="video-js" style="margin: auto auto"></video>
  </div>
</template>
<script setup>
import { onMounted, onUnmounted, ref, defineProps } from "vue";
import Video from "video.js";
import "video.js/dist/video-js.css";

const videoPlayer = ref();
const myPlayer = ref(null);
const props = defineProps({
  videoUrl: String,
  videoImg: String,
});

onMounted(() => {
  onMyPlayer();
});
const onMyPlayer = () => {
  myPlayer.value = Video(
    videoPlayer.value,
    {
      poster: props.videoImg, // 封面
      controls: true,
      sources: [
        {
          src: props.videoUrl, // 视频
          type: "video/mp4",
        },
      ],
      controlBar: {
        remainingTimeDisplay: {
          displayNegative: false,
        },
      },
      playbackRates: [0.5, 1, 1.5, 2],
    },
    () => {
      myPlayer.value.log("play.....");
    }
  );
};
const unMyPlayer = () => {
  if (myPlayer.value) {
    myPlayer.value.dispose();
  }
};
onUnmounted(() => {
  unMyPlayer();
});
</script>

父组件记得加个v-if 

<div v-if="open">
  <VideoDisplay :videoUrl="videoUrl" :videoImg="videoImg" />
</div>

猜你喜欢

转载自blog.csdn.net/weixin_44523517/article/details/126696329