当前位置:   article > 正文

vue.js中vue-video-player中的怎么插入多个视频,视频可以同时播放的问题及解决办法

vue.js中vue-video-player中的怎么插入多个视频,视频可以同时播放的问题及解决办法

多个视频解决问题

因为是从后台获取不同视频数据,要更改playerOptions里面的url,所以一个playerOptions对象没法解决,解决方法:

1. 把playerOptions对象变成空数组
外链图片转存失败,源站可能有防盗在这里插入!链机制,建描述(https://img-blog.cdnimg.cn/2020032011303530.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NTTkQ3OTk3,size_16,color_FFFFFF,t_70)]
2. 从后台获取到的数据放在videolist数组里,如下在这里插入图片描述
3. 循环遍历videolist数组,并定义一个arrs来保存视频播放参数在这里插入图片描述
4. 添加到playerOptions数组里面在这里插入图片描述
5. v-for循环遍历数组,把index索引值也传出来,在video-player标签里要用在这里插入图片描述
大功告成!!!
代码如下:

<ul class="mui-table-view">
      <li class="mui-table-view-cell mui-media" v-for="(item,index) in videolist" :key="item.id">
        <div class="mui-media-body">
          <p class="mui-ellipsis">
            <span>标题:{{ item.title }}</span>
            <span>上传时间:{{ item.time }}</span>
          </p>
        </div>
        <h1>
          <video-player
            class="video-player vjs-custom-skin"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions[index]"
            @play="onPlayerPlay($event,index)"
            @pause="onPlayerPause($event)"
          ></video-player>
        </h1>
      </li>
    </ul>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
 for (var i = 0; i < this.videolist.length; i++) {
            let arrs = {
              playbackRates: [1.0, 2.0, 3.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",
                  type: "video/ogg",
                  src: this.videolist[i].movie //url地址
                }
              ],
              poster: "", //封面地址
              notSupportedMessage: "此视频暂无法播放,请稍后再试", //允许覆盖Video.js无法播放媒体源时显示的默认信息。
              controlBar: {
                timeDivider: true,
                durationDisplay: true,
                remainingTimeDisplay: false,
                fullscreenToggle: true //全屏按钮
              }
            };
            this.playerOptions.push(arrs);
          }
  • 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

视频可以同时播放的问题

可能你们发现了在写 @play="onPlayerPlay($event,index)"时传了一个index属性,这个就是解决同时播放问题的关键。当你们看vue-video-player函数事件的时候,有一个this.$refs.videoPlayer.player.pause()方法就是用来暂停的,当你点击播放的时候会调用 onPlayerPlay,循环遍历 i 不为传过来的index值即可。
在这里插入图片描述
终于解决了!!!

看到有人报错,我就又整理了一遍,并把全部源码发上来。

<template>
  <div>
    <ul class="mui-table-view">
      <li class="mui-table-view-cell mui-media" v-for="(item,index) in videolist" :key="item.title">
        <div class="mui-media-body">
          <p class="mui-ellipsis">
            <span>标题:{{ item.title }}</span>
            <span>上传时间:2020.5.21 13:14:88</span>
          </p>
        </div>
        <h1>
          <video-player
            class="video-player vjs-custom-skin"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions[index]"
            @play="onPlayerPlay($event,index)"
            @pause="onPlayerPause($event)"
          ></video-player>
        </h1>
      </li>
    </ul>
  </div>
</template>
<script>
import { videoPlayer } from "vue-video-player";
require("video.js/dist/video-js.css");
require("vue-video-player/src/custom-theme.css");

export default {
  data() {
    return {
      videolist: [
        { title: "标题A", movie: "../../src/movie/1.mp4" },
        { title: "标题B", movie: "../../src/movie/2.mp4" },
        { title: "标题C", movie: "../../src/movie/3.mp4" },
        { title: "标题D", movie: "../../src/movie/4.mp4" }
      ],
      playsinline: true,
      playerOptions: [],
      options: ""
    };
  },
  created() {
    this.getMovieList();
  },

  methods: {
    getMovieList() {
    // 这里正常来说应该是从后台获取的数据,以下操作都是在成功的回调函数里
      for (var i = 0; i < this.videolist.length; i++) {
          let arrs = {
            playbackRates: [1.0, 2.0, 3.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",
                type: "video/ogg",
                src: this.videolist[i].movie //url地址
              }
            ],
            poster: "", //封面地址
            notSupportedMessage: "此视频暂无法播放,请稍后再试", //允许覆盖Video.js无法播放媒体源时显示的默认信息。
            controlBar: {
              timeDivider: true,
              durationDisplay: true,
              remainingTimeDisplay: false,
              fullscreenToggle: true //全屏按钮
            }
          };
          this.playerOptions.push(arrs);
        }
    },
    onPlayerPlay(player, index) {
 
      
       var that = this.$refs.videoPlayer;
       for (let i = 0; i < that.length; i++) {
          if(i != index)
          that[i].player.pause()
       }
    },
    onPlayerPause(player) {

      
    }
  }
};
</script>
<style lang="scss" scoped>
.mui-table-view {
  h1 {
    font-size: 14px;
    line-height: 24px;
    text-indent: 2em;
  }
  .mui-ellipsis {
    font-size: 14px;
    color: #226aff;
    display: flex;
    justify-content: space-between;
    margin: 5px 0;
  }
  video {
    width: 100%;
    height: 300px;
  }
}
</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
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

并且后台是没有报错的
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/468858
推荐阅读
相关标签
  

闽ICP备14008679号