返回顶部

vue——视频播放插件vue-video-player的使用

参考:https://blog.csdn.net/niexier/article/details/117461895?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2-117461895-blog-80453504.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2-117461895-blog-80453504.pc_relevant_default&utm_relevant_index=5

   https://xianh5.com/archives/12/

      https://blog.csdn.net/qq_33883124/article/details/106807051

 

vue-video-player文档:https://github.com/surmon-china/vue-video-player

 

效果:

     

1. 安装依赖

npm install vue-video-player --save

2. 组件

2.1 播放器组件play-video.vue

<template>
  <el-dialog
    class="dialog-play"
    width="1200px"
    :title="`录屏 - ${title}`"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="close"
    @closed="closed"
  >
    <div class="play-video">
      <video-player
        class="video-player vjs-custom-skin"
        ref="videoPlayer"
        :playsinline="true"
        :options="playerOptions"
        @play="onPlayerPlay($event)"
        @pause="onPlayerPause($event)"
        @ended="onPlayerEnded($event)"
        @waiting="onPlayerWaiting($event)"
        @playing="onPlayerPlaying($event)"
        @loadeddata="onPlayerLoadeddata($event)"
        @timeupdate="onPlayerTimeupdate($event)"
        @canplay="onPlayerCanplay($event)"
        @canplaythrough="onPlayerCanplaythrough($event)"
        @statechanged="playerStateChanged($event)"
        @ready="playerReadied"
      >
      </video-player>
    </div>
  </el-dialog>
</template>

<script>
import 'video.js/dist/video-js.css';
import { videoPlayer } from 'vue-video-player';

export default {
  name: 'play-video',
  components: {
    videoPlayer,
  },
  props: {
    visible: Boolean,
    title: { type: String, default: '' },
  },
  data() {
    return {
      playerOptions: {
        playbackRates: [0.5, 1.0, 2.0], // 可选的播放速度
        autoplay: true, // 如果为true,浏览器准备好时开始回放。
        muted: true, // 默认情况下将会消除任何音频。
        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', // 类型
            src: 'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4', // url地址,若为后端返回,需为文件流
          },
        ],
        poster: '', // 封面地址,不设置会默认第一帧为封面
        notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: true, // 是否显示剩余时间功能
          fullscreenToggle: true, // 是否显示全屏按钮
        },
      },
    };
  },
  computed: {
    player: {
      get() {
        return this.$refs.videoPlayer.player;
      },
      set(newValue) {
        return newValue;
      },
    },
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.playerOptions.sources[0].src =
          'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4';
        this.playerOptions.poster =
          'https://img1.wxzxzj.com/vpc-example-cover-your-name-c.png';
      }
    },
  },
  methods: {
    close() {
      this.$emit('close');
    },
    // 弹窗关闭后再重置数据
    closed() {
      this.playerOptions.sources[0].src =
        'http://static.smartisanos.cn/common/video/t1-ui.mp4';
      this.playerOptions.poster =
        'https://img1.wxzxzj.com/vpc-example-cover-your-name-c.png';
    },
    // 播放回调
    onPlayerPlay(player) {
      console.log('player play!', player);
    },
    // 暂停回调
    onPlayerPause(player) {
      console.log('player pause!', player);
    },
    // 视频播完回调
    onPlayerEnded($event, player) {
      console.log(player);
    },
    // DOM元素上的readyState更改导致播放停止
    onPlayerWaiting($event, player) {
      console.log(player);
    },
    // 已开始播放回调
    onPlayerPlaying($event, player) {
      console.log(player);
    },
    // 当播放器在当前播放位置下载数据时触发
    onPlayerLoadeddata($event, player) {
      console.log(player);
    },
    // 当前播放位置发生变化时触发。
    onPlayerTimeupdate($event, player) {
      console.log(player);
    },
    // 媒体的readyState为HAVE_FUTURE_DATA或更高
    onPlayerCanplay(player) {
      // console.log('player Canplay!', player)
    },
    // 媒体的readyState为HAVE_ENOUGH_DATA或更高。这意味着可以在不缓冲的情况下播放整个媒体文件。
    onPlayerCanplaythrough(player) {
      // console.log('player Canplaythrough!', player)
    },
    // 播放状态改变回调
    playerStateChanged(playerCurrentState) {
      console.log('player current update state', playerCurrentState);
    },
    // 将侦听器绑定到组件的就绪状态。与事件监听器的不同之处在于,如果ready事件已经发生,它将立即触发该函数。。
    playerReadied(player) {
      console.log('example player 1 readied', player);
    },
  },
};
</script>
<style lang="less" scoped>
@deep: ~'>>>';
.dialog-play {
  @{deep}.el-dialog__body {
    padding: 0;
  }
  .play-video {
    width: 100%;
    margin: 0 auto;
    text-align: center;
    @{deep}.video-js {
      .vjs-big-play-button {
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
      .vjs-current-time,
      .vjs-time-divider,
      .vjs-duration {
        display: block;
        padding-left: 0.3em;
        padding-right: 0.3em;
      }
      .vjs-remaining-time {
        display: none;
      }
    }
  }
}
</style>

2.2 调用播放器组件list.vue

...    
<el-button @click="playVideo(scope.row)"
>播放</el-button>
...
<play-video
      :visible="showVideo"
      :title="fileName"
      :append-to-body="true"
      @close="playVideoShowChange"
></play-video>
...

...
 // 关闭播放弹窗
playVideoShowChange() {
    this.showVideo = false;
},
// 播放
playVideo(data) {
    this.fileName = data.fileName;
    this.showVideo = true;
},
...

 3. 播放flv格式视频

3.1 安装flash依赖

npm install videojs-flash --save

3.2 播放器组件中引入

import 'videojs-flash';

3.3 播放器配置修改

...
sources: [{
    type: 'video/x-flv',  // <<< 关键
    src: require('./1.flv'), // 播放本地flv视频
}],
techOrder: ['flash'], // <<< 关键
...

3.4 遇到的问题

报错1 :

Module parse failed: Unexpected character '' (1:3)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

解决方法:

修改webpack.configbase.js

原:

  module: {
    rules: [
        ...
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: assetsPath('media/[name].[hash:7].[ext]'),
          esModule: false, // 默认为true 会将img的src设置为[object Module]
        },
      },
        ...
}

改:

test: /\.(mp4|flv|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, // 增加flv

 

报错2:

VIDEOJS: ERROR: The "flash" tech is undefined. Skipped browser support check for that tech.

原因:

videojs和videojs-flash里的各有一个video.js,如果两个版本不一样可能就会报错了。
 
解决方法:
修改webpack.configbase.js
resolve: {
    ...
   modules: [path.resolve('node_modules'), 'node_modules'], // 配置第三方模块的查找顺序,优先查找本身安装的videojs
    ...
}

报错3:

flash插件不支持

解决方法:

使用360或其他支持flash的浏览器,或安装flash(我暂时是使用360可以)

posted @ 2022-05-06 16:57  前端-xyq  阅读(3543)  评论(0编辑  收藏  举报