在vue项目中,使用videoPlayer插件,播放hls m3u8格式的视频流

安装videoPlayer
yarn add vue-video-player -S
播放hls的话需要安装videojs-contrib-hls 并在组件内引入
yarn add videojs-contrib-hls

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

<script>
import { videoPlayer } from "vue-video-player";
import "video.js/dist/video-js.css";
import "@/utils/global.js";
export default {
  props: {
    videoData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      playerOptions: {
        controls: true,
        playbackRates: [0.5, 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", // 类型
            src: "http://videocdn.renrenjiang.cn/Act-ss-m3u8-sd/1037359_1546064640169/1037359_1546064640169.m3u8", // url地址
          },
        ],
        poster: "", // 封面地址
        notSupportedMessage: "此视频暂无法播放,请稍后再试", // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: true, // 是否显示剩余时间功能
          fullscreenToggle: true, // 是否显示全屏按钮
        },
      },
    };
  },
  components: {
    videoPlayer,
  },
  methods: {},
  mounted() {
    //   this.inintPlayer()
  },
};
</script>

<style lang="less" scoped>
.videoBox {
  position: relative;
  // background-color: #ccc;
  width: 100%;
  height: 240px;
  display: flex;
  justify-content: center;
  align-items: center;
  .video-player {
    //   width: 100%;
    padding: 20px;
    flex: 1;
  }
}
</style>

版权声明:本文为weixin_55195734原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。