封装videoJS,可以作为Vue组件使用

封装子组件

<script>
// 引入video.js先关的js和css
import videojs from 'video.js'
import "video.js/dist/video-js.min.css"
import video_zhCN from 'video.js/dist/lang/zh-CN.json'
videojs.addLanguage('zh-CN', video_zhCN)
export default {
  name: 'VideoJs',
  props: {
    url: {
      // 父组件传过来的视频链接
      type: String,
      default: '',
    },
    id: {
      // 父组件传过来的视频序号
      type: String,
      default: null,
    },
  },
  mounted(){
    // 添加初始化宽和高,否则加载播放器的时候会闪一下
    let options = {
      width:960,
      height:400,
      language: "zh-CN",
    }
    let that = this
    let player = videojs(this.$refs.videojs, options, function onPlayerReady() {
      videojs.log('播放器准备好了!');

      // 最新的浏览器一般禁止video自动播放,直接调用play()会报错。只有用户在页面上操作后或者在video标签上添加muted(静音)属性,才能调用play函数。本案例是在video标签上添加了muted属性。
      this.on('ended', function() {
        videojs.log('播放结束!');
        that.$emit('ended', that.id)
      });
      // 解决出现因网络问题需要重新加载视频问题,提示错误:A network error caused the media download to fail part-way.
      this.reloadSourceOnError({
        // getSource allows you to override the source object used when an error occurs
        getSource: function(reload) {
          console.log('Reloading because of an error');

          // call reload() with a fresh source object
          // you can do this step asynchronously if you want (but the error dialog will
          // show up while you're waiting)
          reload({
            src: that.url + '?rnd=' + new Date().getTime()
            // type: 'application/x-mpegURL'
          });
        },

        // errorInterval specifies the minimum amount of seconds that must pass before
        // another reload will be attempted
        errorInterval: 5
      });
    }, {passive: false});
  }
}
</script>

<template>
  <div>
    <video
      ref="videojs"
      class="video-js"
      controls
      preload="auto"
      data-setup='{}'
      muted
    >
      <source :src="this.url + '?rnd=' + new Date().getTime()" type="video/mp4"/>
    </video>
  </div>
</template>
<style scoped>
.video-js {
  width: 100% !important;
  height: 100% !important;
}
</style>


在父组件引入使用:

<VideoJs style="width: 100%;height: 100%;" :id="'video' + index" @ended="endedHandle" :url="ossUrl + item.url"></VideoJs>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是从零开始封装一个基于videojsVue的动态source的视频播放器组件的步骤: 1. 安装video.jsvue-video-player 使用npm安装video.jsvue-video-player: ``` npm install video.js vue-video-player --save ``` 2. 创建Vue组件 在src/components目录下创建一个VideoPlayer.vue的文件,该文件包含以下内容: ```html <template> <div> <video ref="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered"></video> </div> </template> <script> import videojs from 'video.js'; import 'video.js/dist/video-js.css'; import 'vue-video-player/src/custom-theme.css'; import 'vue-video-player/src/font/font.css'; import 'vue-video-player/src/assets/video-player.css'; export default { name: 'VideoPlayer', props: { sources: { type: Array, required: true }, options: { type: Object, default: () => ({}) } }, mounted() { this.initPlayer(); }, beforeDestroy() { if (this.player) { this.player.dispose(); } }, methods: { initPlayer() { const video = this.$refs.videoPlayer; const playerOptions = Object.assign({ controlBar: { children: { playToggle: {}, progressControl: {}, remainingTimeDisplay: {}, fullscreenToggle: {} } }, sources: this.sources }, this.options); this.player = videojs(video, playerOptions, function onPlayerReady() { console.log('Player ready'); }); } } }; </script> ``` 3. 在Vue项目中使用组件 在需要使用视频播放器的Vue组件中引入VideoPlayer组件,并传入sources和options参数: ```html <template> <div class="app"> <VideoPlayer :sources="sources" :options="options"></VideoPlayer> </div> </template> <script> import VideoPlayer from '@/components/VideoPlayer.vue'; export default { name: 'App', components: { VideoPlayer }, data() { return { sources: [ { src: 'http://example.com/path/to/video.mp4', type: 'video/mp4' } ], options: { autoplay: false, controls: true, preload: 'auto', fluid: true } }; } }; </script> ``` 这样就完成了从零开始封装一个基于videojsVue的动态source的视频播放器组件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值