1. 程式人生 > >vue專案利用vue-video-player實現視訊相容IOS和安卓放大播放

vue專案利用vue-video-player實現視訊相容IOS和安卓放大播放

首先,圖片和視訊混合一起,預設圖片製作poster點選彈窗手機播放器

<template>
  <div class="look-v">
    <section class="view-wrap" v-if="accObj">
      <div class="v-im" :class="[{'sub-center': accObj.coursewareList.length%4 == 0||accObj.coursewareList.length == 10},{'justCenter':accObj.coursewareList.length <3}]">
        <div v-if="accObj.coursewareList.length != 0" v-for="(item,index) in accObj.coursewareList" :key="index">
          <div class="view" v-if="item.type != 'mp4'" @click="toIndexbig(accObj.coursewareList,item,index)">
            <img class="img" ref="img" v-if="item.coursewarePath" :src="`${item.coursewarePath}?x-oss-process=image/resize,l_100`" :onerror="`this.src = '${defaultImg}`" alt="">
            <span class="error-tip" v-else>暫無圖片</span>
          </div>
          <div v-else class="v-vo" @click='toMovie(item)'>
            <img class="img" ref="img" v-if="item.coursewareName" src="../../../static/images/play.png">
            <span class="error-tip" v-else>暫無圖片</span>
          </div>
        </div>
        <div class="v-dec" v-if="accObj.coursewareList[0].type != 'mp4'">
          <i class="icon icon-big"></i>
          <span>點選圖片放大</span>
        </div>
      </div>
    </section>
    <div class="video-player-box" @click="showVideo('none')">
      <i class="close"></i>
      <video-player @error="onError($event)" @play="onPlayerPlay($event)" @pause="onPlayerPause($event)" @statechanged="playerStateChanged($event)" @ended="onPlayerEnded($event)" :options="playerOptions" :playsinline="true" ref="videoPlayer" />
    </div>
  </div>
</template>

<script>
import { videoPlayer } from "vue-video-player";
import "../../../static/css/video-js.css";
require("videojs-contrib-hls");
export default {
  props: ["accObj"],
  components: { videoPlayer },
  data() {
    return {
      defaultImg: require('../../../static/images/default.png'),
      playerOptions: {},
      playerOption: {
        // playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
        autoplay: true, //如果true,瀏覽器準備好時開始回放。
        muted: false, // 預設情況下將會消除任何音訊。
        loop: false, // 導致視訊一結束就重新開始。
        preload: "auto", // 建議瀏覽器在<video>載入元素後是否應該開始下載視訊資料。auto瀏覽器選擇最佳行為,立即開始載入視訊(如果瀏覽器支援)
        language: "zh-CN",
        // aspectRatio: '4:3', // 將播放器置於流暢模式,並在計算播放器的動態大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數字(例如"16:9"或"4:3")
        // fluid: true, // 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。
        poster: "", //你的封面地址
        width: document.documentElement.clientWidth,
        notSupportedMessage: "此視訊暫無法播放,請稍後再試", //允許覆蓋Video.js無法播放媒體源時顯示的預設資訊。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true //全屏按鈕
        }
      }
    };
  },
  methods: {
    toMovie(item) {
      if (item.coursewarePath) {
        this.showVideo("block");
        const type = item.coursewarePath.split("/");
        if (type[type.length - 1].indexOf(".m3u8") >= 0) {
          this.playerOptions = {
            sources: [
              {
                src: item.coursewarePath,
                type: "application/x-mpegURL"
              }
            ],
            ...this.playerOption
          };
        } else {
          this.playerOptions = {
            sources: [
              {
                src: item.coursewarePath,
                type: "video/mp4"
              }
            ],
            ...this.playerOption
          };
        }
      }
    },
    showVideo(type) {
      document.querySelector(".video-player-box").style.display = type;
      if (type == "none") {
        const Player = this.$refs.videoPlayer.player;
        Player.pause();
      }
    },
    toIndexbig(obj,item,index) {
      let viewIndex = obj.filter(item =>{return item.type != 'mp4'}).indexOf(item);
      this.accObj.viewindex = viewIndex;
      this.$emit("toIndexbig", this.accObj);
    },
    onPlayerPlay(player) {
       //全屏播放
      if (!player.isFullscreen()) {
        player.requestFullscreen();
        player.isFullscreen(true);
      } else {
        player.exitFullscreen();
        player.isFullscreen(false);
      }
    },
    playerStateChanged(player) {
      // console.log(player);
    },
    onPlayerPause(player) {
      console.log("Pause");
    },
    onPlayerEnded(player) {
      console.log("end");
    },
    onError(player, item, index) {
      // this.optionArr[index].sources[0].src = item.coursewarePath;
    },
    getFileUrl(item) {
      var arr = item.coursewarePath.split("/");
      var fileName = arr[arr.length - 1];
      return `static/images/${fileName}`;
    }
  }
};
</script>

 

播放器div巢狀fixed浮在表面