<template>
  <div id="app">
    <h1>常规、可播m3u8</h1>
    <video-player
      class="video-player-box vjs-custom-skin"
      ref="videoPlayer"
      :options="playerOptions"
      :playsinline="true"
    >
    </video-player>

    <h1>常规、可播m3u8、可做防盗链</h1>
    <video-player
      class="video-player-box2 vjs-custom-skin"
      ref="videoPlayer"
      :options="playerOptions"
      :playsinline="true"
      @ready="playerReadied"
    >
    </video-player>
  </div>
</template>

<script>
import VideoPlayer from "./player.vue";
import md5 from "md5";
import "./custom-theme.css";
const vUrl = "https://d2zihajmogu5jn.cloudfront.net";

export default {
  name: "app",
  data() {
    return {
      playerOptions: {
        // videojs options
        allowUpdate: false, // 设置false，禁止指令强制重载组件。
        muted: true,
        language: "en",
        playbackRates: [0.7, 1.0, 1.5, 2.0],
        sources: [
          {
            type: "application/x-mpegURL",
            src: this.uptUpdate("/bipbop-advanced/bipbop_16x9_variant.m3u8")
          }
        ]
      }
    };
  },
  components: {
    VideoPlayer
  },
  methods: {
    uptUpdate(path) {
      let secret = md5(new Date().getTime());
      return vUrl + path + "?token=" + secret;
    },
    playerReadied() {
      let that = this;
      this.player.hls.xhr.beforeRequest = function(options) {
        options.uri = that.uptUpdate(options.uri.replace(vUrl, ""));
        console.info("防盗链token模拟", options.uri);
        return options;
      };
    }
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player;
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.video-player-box .video-js {
  margin: 0 auto;
  width: 796px;
  height: 448px;
}
.video-player-box2 .video-js {
  margin: 0 auto;
  width: 796px;
  height: 448px;
  margin-top: 50px;
}
</style>
