videoDialog.vue 6.98 KB
<template>
  <el-dialog :title="$t('dialog.video')" :visible.sync="isShow" v-if="isShow" :close-on-click-modal='false' class="manage-dialog dialog_lj" :before-close="closeDialog" width="1200px">
    <div v-show="platform==1" class="video-box" style="height: 700px;" v-loading="loading">
    	<canvas id="canvas" style="background: black;width:100%;height:100%"></canvas>
    </div>
    <!-- poster="../../images/huiyi.jpg" -->
    <div v-show="platform==2" style="height:'700px'" v-loading="loading">
       <div id="my-video" style="background: black;width:100%;height:100%"></div>
      <!-- <el-radio-group v-model="radio1" @change="radioChange">
        <el-radio-button label="高清"></el-radio-button>
        <el-radio-button label="流畅"></el-radio-button>
      </el-radio-group> -->
      <!-- <video style="height: 700px;width: 100%;"  id="myVideo" class="video-js">
           <source :src="palyUrl" type="application/x-mpegURL">
       </video> -->
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" class="dialog-btn">{{$t('dialog.cancel')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import EZUIKit from 'ezuikit-js'
  // import videojs from 'video.js'
  // import 'videojs-contrib-hls'
  // import 'video.js/dist/video-js.css'
  import SLPlayer from '../../../../static/js/slplayer'
  export default {
    data() {
      return {
        currObj:{
          radio:1
        },
        radio1:'高清',
        loading: true,
        isShow: false,
        Module:{},
        player:'',
        platform:'',
        palyUrl:'',
        playerM3u8:'',
        playerM3u8Index:0,
        playerEz:''
      }
    },
    destroyed() {
    	if(this.platform == 1){
        this.player.stop()
      }else if(this.platform == 2){
        if(this.playerEz){
          let canvasVideo = document.getElementById('my-video')
          canvasVideo.innerHTML = ''
        }
        // this.playerM3u8.dispose()
      }
    },
    methods: {
      radioChange(val){
        this.currObj.radio = this.radio1=='高清'?'1':'2';
        this.dialogInit(this.currObj)
      },
      dialogInit(row,val){
        if(val){
          this.playerM3u8Index = 0
          this.radio1=='高清'
        }
        this.currObj = {
          ...row
        }
        let parmas = {
          id:row.id
        }
        if(row.platform==2){
          parmas.protocol=1
          // parmas.quality = this.currObj.radio?this.currObj.radio:'1'
        }
        this.loading = true
        this.$api.videoShopTourReport.getLiveAddress(parmas).then(data => {
          let result = data.data;
          if(result.code==200){
            this.playVideo(result.data.liveAddress,row.platform,result.data.accessToken);
          }
        })
      },
      playVideo(row,platform,atoken) {
        this.platform = platform
        this.isShow = true;
        this.loading = false
        // this.createPlayer(row);
        this.$nextTick(()=>{
          if(this.platform==1){
            this.createPlayer(row);
          }else{
            this.palyUrl= row
            if(this.playerEz){
              let canvasVideo = document.getElementById('my-video')
              canvasVideo.innerHTML = ''
            }
            // 开始萤石云视频
            this.playerEz = new EZUIKit.EZUIKitPlayer({
              id: "my-video", // 视频容器ID
              accessToken: atoken,
              url: this.palyUrl,
              template: 'simple',//simple - 极简版;standard-标准版;security - 安防版(预览回放);voice-语音版;
              autoplay: true,
              footer: ['hd','fullScreen'],
              width: document.getElementById('my-video').offsetWidth - 20,
              height:700,
            });
            // if(this.playerM3u8Index>0){
            //   setTimeout(()=>{
            //     if(this.playerM3u8){
            //       this.playerM3u8.src({
            //         src:this.palyUrl,
            //         type:'application/x-mpegURL'
            //       })
            //       this.playerM3u8.load()
            //     }
            //   },1000)
            // }else{
            //   setTimeout(()=>{
            //     this.playerM3u8Index++;
            //     this.playerM3u8 = videojs(document.getElementById('myVideo'), {   //播放的事件
            //         textTrackDisplay: false,
            //         posterImage: false,
            //         muted: false,
            //         controls: true,
            //         autoplay:true,
            //         preload:'auto',
            //         hls: {
            //           withCredentials: true
            //         },
            //         controlBar:{
            //           fullscreenToggle :false,
            //           pictureInPictureToggle: false,
            //         }
            //       },function(){
            //         console.log('视频可以播放了',this)
            //       })
            //   },1000)
            // }
          }
        })
      },
      createPlayer(row) {
        let that = this;
        that.Module = {
        	/* ************************************ */
        	// 定义视频画布
        	canvas: document.getElementById("canvas"),
        	/* ************************************ */
        	// 设置相应的事件回调
        	onStart: function () {
        		console.log("onStart...")
        	},
        	onEnd: function () {
        		console.log("onEnd...")
        	},
        	onError: function (code, err) {
        		alert('error: ', code, err)
        		console.log('error: ', code, err);
        	},
        	onVideoStart: function () {
        		console.log('onVideoStart。。。');
        	},
        	// 录像回放中返回时间刻度
        	onPlayTime: function (ts) {
        		if(ts==0) return;
        		var date = new Date(ts * 1000);//如果date为10位不需要乘1000
        		var Y = date.getFullYear() + '-';
        		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        		var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        		var timestamp = Y + M + D + h + m + s;
        	}
        }
        SLPlayer(that.Module).then(function (obj) {
          that.loading = false
          that.player = obj;
          setTimeout(()=>{
            console.log(111)
            that.player.start(row)
          },1000)
       });
      },
      closeDialog() {
        if(this.platform == 1){
          this.player.stop()
        }else if(this.platform == 2){
          // this.playerM3u8.dispose()
          if(this.playerEz){
            let canvasVideo = document.getElementById('my-video')
            canvasVideo.innerHTML = ''
          }
        }
        this.isShow = false;
      }
    }
  }
</script>