当前位置:   article > 正文

vue3播放m3u8视频(videoJS)

vue3播放m3u8

安装

npm install video.js --save
  • 1

组件内导入

需要同时导入videoJS以及相关的CSS

import videojs from "video.js"
import "video.js/dist/video-js.css"
  • 1
  • 2

组件内使用

import { onUnmounted, ref, nextTick } from "vue"
import videojs from "video.js"
import "video.js/dist/video-js.css"
const videoPlayer = ref(null)
const myPlayer = ref(null)
nextTick(() => {
  myPlayer.value = videojs(videoPlayer.value, {
    // poster: "//vjs.zencdn.net/v/oceans.png",//视频封面照片
    controls: true,//视频控件
    autoplay: true,//自动播放
    sources: [
      {
        src: `https://video.m3u8`,//播放视频地址
        type: 'application/x-mpegURL',//播放m3u8需要设置的格式
      }
    ],
    controlBar: {
      remainingTimeDisplay: {
        displayNegative: false
      }
    },
    playbackRates: [0.5, 1, 1.5, 2]//设置播放速度
  },)
})
//页面销毁时,销毁Video播放组件
onUnmounted(() => {
  if (myPlayer.value) {
    myPlayer.value.dispose()
  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

组件示例

以下时项目使用中自己封装的组件,用起来也很简单,需要的可以对照自取
UI框架为 ant-design-vue


<template>
  <div class="video_wrap">
    <div class="backIndex">
      <span @click="router.push({ path: '/videoList' })"><left-outlined /></span>
    </div>
    <video ref="videoPlayer" muted="muted" class="video-js video"></video>
  </div>
</template>
<script setup>
import { onUnmounted, ref, nextTick } from "vue"
import { useRoute, useRouter } from "vue-router";
import { LeftOutlined } from "@ant-design/icons-vue";
import videojs from "video.js"
import "video.js/dist/video-js.css"
const videoPlayer = ref(null)
const myPlayer = ref(null)
const route = useRoute();
const router = useRouter()
let token = route.query.token;
nextTick(() => {
  myPlayer.value = videojs(videoPlayer.value, {
    // poster: "//vjs.zencdn.net/v/oceans.png",//视频封面照片
    controls: true,//视频控件
    autoplay: true,//自动播放
    sources: [
      {
        src: `/api/video/playlist/${token}.m3u8`,//视频地址
        type: 'application/x-mpegURL',
      }
    ],
    controlBar: {
      remainingTimeDisplay: {
        displayNegative: false
      }
    },
    playbackRates: [0.5, 1, 1.5, 2]//设置播放速度
  },)
})

onUnmounted(() => {
  if (myPlayer.value) {
    myPlayer.value.dispose()
  }
})
</script>
<style lang="scss" scoped>
.video_wrap {
  width: 100vw;
  height: 100vh;
  position: relative;

  .backIndex {
    position: absolute;
    top: 0;
    left: 0;
    height: 50px;
    width: 100%;
    line-height: 50px;
    background: rgba(0, 0, 0, .5);
    z-index: 99;
    padding-left: 10px;
    font-size: 20px;
    font-weight: 400;
    opacity: 0;
    transition: all .3s;
    color: white;

    &:hover {
      opacity: 1;
    }

    span {
      cursor: pointer;
    }
  }

  .video {
    height: 100%;
    width: 100%;
  }

  ::v-deep(.vjs-big-play-button) {
    margin-left: 45%;
    margin-top: 20%;
  }
}
</style>



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/92446
推荐阅读
相关标签
  

闽ICP备14008679号