vue 视频播放器 支持m3u8

vue yekong 1998℃

下载vue-video-player

npm install vue-video-player --save

在main.js文件引入

import VideoPlayer from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')
Vue.use(VideoPlayer)

使用

<video-player
 ref="videoPlayer"
 class="video-player vjs-custom-skin"
 :playsinline="true"
 :options="playerOptions"
 @play="onPlayerPlay($event)"//监听开始状态
 @pause="onPlayerPause($event)"//监听暂停状态
/>

data

playerOptions: {
   // playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
   autoplay: false, // 如果true,浏览器准备好时开始回放。
   muted: false, // 默认情况下将会消除任何音频。
   loop: false, // 导致视频一结束就重新开始。
   preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
   language: 'zh-CN',
   aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
   fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
   sources: [
     {
      type: 'video/mp4', // 这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目
      src: 'https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm' // url地址
     }
   ],
   hls: true,
   poster: 'http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg', // 你的封面地址
   width: document.documentElement.clientWidth, // 播放器宽度
   notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
   controlBar: {
    timeDivider: true,
    durationDisplay: true,
    remainingTimeDisplay: false,
    fullscreenToggle: true // 全屏按钮
   }
  },
  
  computed: {
 player() {
  return this.$refs.videoPlayer.player//自定义播放
 }
},

兼容m3u8的需要下载

npm install --save videojs-contrib-hls

引入

import hls from 'videojs-contrib-hls'
Vue.use(hls)

实际使用代码

<template>
  <div class="popvideos" v-if="show">
    <div class="popvideos2">
      <i class="el-icon-circle-close cur" @click="show=false"></i>
      <video-player
        ref="videoPlayer"
        class="video-player vjs-custom-skin"
        :playsinline="true"
        :options="playerOptions"
        v-if="url"
      />
      <div class="popimg">
        <img @click="getactive(index)" :class="{active:active==index}" v-for="(item,index) in list"
             :key="index" src="../../assets/shipintupianimg.png" alt="">
      </div>
    </div>
  </div>
</template>

<script>

import {interface_video} from "../../api/api/zixun";

export default {
  name: "popvideo",
  components: {},
  props: {
    list: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      show: false,
      active: 0,
      url: false,
      playsinline: true,
      playerOptions: {
        // playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
        autoplay: true, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [
          {
            type: 'application/x-mpegURL', // 这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目
            src: '' // url地址
          }
        ],
        hls: true,
        poster: '', // 你的封面地址
        width: document.documentElement.clientWidth, // 播放器宽度
        notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true // 全屏按钮
        }
      },
    }
  },
  watch: {},
  mounted() {
  },
  created() {
  },
  methods: {
    getshow() {
      this.show = true
      this.getdata1()
    },
    getdata1() {
      var that = this
      var id = this.list[0].key
      interface_video(id).then(function (res) {
        if (res.code == 0) {
          that.url = false
          that.$nextTick(() => {
            that.url = res.data.url
            that.playerOptions.sources[0].src = that.url
            // that.playerOptions.sources[0].src = 'http://demo.wanjunshijie.com/hst/mhtml/video/movie.mp4'
          })
        }
      })
    },
    getactive(e) {
      this.active = e;
      this.getdata2()
    },
    getdata2() {
      var that = this
      var id = that.list[this.active].key
      interface_video(id).then(function (res) {
        if (res.code == 0) {
          that.url = false
          that.$nextTick(() => {
            that.url = res.data.url
            that.playerOptions.sources[0].src = that.url
            // that.playerOptions.sources[0].src = 'http://demo.wanjunshijie.com/hst/mhtml/video/movie.mp4'
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.videosp {
  width: 400px;
  height: 225px;
  object-fit: fill;
}

.popvideos {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  position: fixed;
  z-index: 10000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.popvideos2 {
  width: 400px;
  background: #144A9C;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: column;
  position: relative;
}

.popvideos2 {
  .el-icon-circle-close {
    position: absolute;
    right: 10px;
    top: 10px;
    color: #fff;
    font-size: 20px;
    z-index: 1000000;
  }
}

.popimg {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  height: 46px;

  img {
    width: 60px;
    height: 34px;
    margin-left: 3px;
    margin-right: 3px;
    cursor: pointer;
    border: 1px solid rgba(#00EAFF, 0);
  }

  img.active {
    border: 1px solid rgba(#00EAFF, 1);
  }
}

.video-player {
  position: relative;
  z-index: 10000;
  width: 400px;
  height: 225px;
}
</style>

喜欢 (0)