<template>
  <div style="max-width: 640px;margin:10% auto;">
    <div class="video-box">
      <video-player
        class="video-player vjs-custom-skin"
        ref="videoPlayer"
        :playsinline="true"
        @pause="onPlayerPause($event)"
        @play="onPlayerStart($event)"
        @ready="playerReadied"
        @ended="onPlayerEnded($event)"
        :globalOptions="{controls:true}"
        :options="options">
      </video-player>
    </div>
    <div style="margin:10px auto;">
      <el-button @click="set360">360</el-button>
      <el-button @click="set640">640</el-button>
      <el-button @click="set960">960</el-button>
    </div>

  </div>
</template>

<script>
  import {videoPlayer } from 'vue-video-player';
  import 'video.js/dist/video-js.css'
  import { MessageBox } from "element-ui";
  export default {
    name: "Index",
    data() {
      let id = this.$route.params.id||1001
      return {
        id,
        timer: false,
        playtimes: 0,
        tickNum: 0,
        curIndex:0,
        onPlay: false,
        isfullScreen:false,
        events: ['fullscreenchange'],
        options:{
          autoplay: true, // 如果true,浏览器准备好时开始回放。
          muted: false, // 默认情况下将会消除任何音频。
          loop: false, // 导致视频一结束就重新开始。
          preload: "true", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
          language: 'zh-CN',
          aspectRatio: '4:3', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
          fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
          sources: [
            {
              src:`http://media.hqedust.com:8889/hls/640/${id}/${id}.m3u8`
            }
          ],
          poster: '', // 你的封面地址
          notSupportedMessage: '无法播放媒体源', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
          playtimes: '',
          controlBar: {
            currentTimeDisplay: true, // 当前时间
            timeDivider: true, // 时间分割线
            durationDisplay: true, // 总时间
            progressControl: true, // 进度条
            remainingTimeDisplay: true, //
            customControlSpacer: true, //
            fullscreenToggle: true, // 全屏按钮
            volumePanel: false
          }
        }
      }
    },
    components: {
      videoPlayer
    },
    computed: {
      player() {
        return this.$refs.videoPlayer.player
      }
    },
    methods: {
      onPlayerPause(event) {
        this.onPlay = false
      },
      playerReadied(player) {
        let myPlayer = this.$refs.videoPlayer.player;
        myPlayer.currentTime(this.playtimes);
      },
      set360(){
        let id = this.id
        let myPlayer = this.$refs.videoPlayer.player;
        this.playtimes  = myPlayer.currentTime();
        myPlayer.src( `http://media.hqedust.com:8889/hls/360/${id}/${id}.m3u8` )
      },
      set640(){
        let id = this.id
        let myPlayer = this.$refs.videoPlayer.player;
        this.playtimes  = myPlayer.currentTime();
        myPlayer.src( `http://media.hqedust.com:8889/hls/640/${id}/${id}.m3u8` )
      },
      set960(){
        let id = this.id
        let myPlayer = this.$refs.videoPlayer.player;
        this.playtimes  = myPlayer.currentTime();
        myPlayer.src( `http://media.hqedust.com:8889/hls/960/${id}/${id}.m3u8` )
      },

      doPause( ){
        let myPlayer = this.$refs.videoPlayer.player;
        myPlayer && myPlayer.pause()
        this.onPlay = false
      },
      doPlay(){
        if( !this.$refs.videoPlayer || !this.$refs.videoPlayer.player ) return;
        if( !this.dialog ) return this.doPause();
        let myPlayer = this.$refs.videoPlayer.player;
        myPlayer && myPlayer.play()
        this.tickNum = 0
        this.onPlay = true
      },
      onPlayerStart() {
        this.onPlay = true
      }
    }
  }
</script>

<style>
  .video-js{
    .vjs-control-bar{
      .vjs-icon-custombutton {
        font-family: VideoJS;
        font-weight: normal;
        font-style: normal;
       }
      .vjs-icon-custombutton:before {
        content: "\f108";
        font-size: 1.8em;
        line-height: 1.67;
      }
    }
  }
  .p-process{
    width: 100%;
    margin:20px auto;
    height: 30px;
  }
</style>
