vue-video-player播放m3u8格式视频详解(版本有坑)

下载vue-video-player及videojs-contrib-hls

// 记住下载这两个包就够了,千万不要下载video.js,因为vue-video-player会自动下载video.js,不然会冲突
// videojs-contrib-hls该组件是要兼容m3u8格式视频才需要的,若是一般的mp4文件,则不需要
npm install vue-video-player@5.0.1 -save
npm install videojs-contrib-hls@5.15.0 -save

在main.js里面导入

//引入video插件
import VideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'
//引入 hls
import 'videojs-contrib-hls'
Vue.use(VideoPlayer)

封装组件

<template>
  <div class="vueVideoPlayer">
    <video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions">
    </video-player>
  </div>
</template>

<script>

export default {
  name: 'vueVideoPlayer',
  props: {
    src: {
      type: String
    },
    cover_url: {
      type: String
    },
    autoplay: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 配置信息
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: this.autoplay, // 如果true,浏览器准备好时自动播放视频
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
        language: 'zh-CN',
        aspectRatio: '16:10', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [{
          withCredentials: true,
          type: "application/x-mpegURL", // m3u8视频的类型,而mp4的为video/mp4
          // src: "/hls/live/1/index.m3u8",
          src: this.src
        }],
        // poster: this.cover_url, // 你的封面地址
        width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 是否显示全屏按钮
        }
      }
    }
  },
  methods: {},
  mounted () {
    console.log(this.src);
  }
}
</script>

<style lang="less">
.vueVideoPlayer {
  & .video-js {
    & .vjs-big-play-button {
      top: 49%;
      left: 46%;
    }
  }
}
</style>
  • 注:可能会出现跨域问题,自行解决
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值