vue+videojs+videojs-contrib-hl实现hls视频流切换播放

vue+videojs+videojs-contrib-hl实现点击勾选不同的数据,播放不同的视频。

<template>
  <div class="player-container">
    <template v-if="hlsurl">
      <div id="video-content">
        <video 
          :id="'myvideo'+cameraId"
          class="video-js vjs-default-skin" 
          controls 
          preload="auto" 
          poster>
            <source 
              :src="hlsurl" 
              type="application/x-mpegURL">
        </video>
      </div>
    </template>
     <template v-if="!hlsurl">
       <img src="/public/images/warning.png"/><span>该设备暂无数据</span>
     </template>
  </div>
</template>

注意:在加载下一个的时候,必须先dispose,不然会一直加载。dispose销毁会将页面的代码一起销毁,因为直接重建会报错,所以需要通过js将代码添上去:

 var videoContent = document.getElementById("video-content");
                      videoContent.innerHTML = " ";  

下面是完整的代码:

<script>
import videojs from 'video.js'
import 'videojs-contrib-hls'

  export default {
    props:{
      hlsurl:"",
      cameraId:"",
      isChangeType:false,
    },
    data () {
      return {
        videoUrl:"",
        videoId:"",
        videoType:"", 
        videoPlayer:null,
        isVideoTypeChange:0,
      }
    },
    watch:{
      hlsurl: {
        immediate: true,
        handler (val) {
          this.videoUrl= val;
        }
      },
      cameraId: {
        immediate: true,
        handler (val) {
            this.videoId= val;
        }
      },
      isChangeType: {
        immediate: true, 
        handler (val) {            
            this.isVideoTypeChange = val;       
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
          this.$watch(function() {
            return {url: this.videoUrl, id: this.videoId,type:this.videoType}
          }, function(params) {
                if(this.videoUrl){
                    if(this.videoPlayer){
                      this.videoPlayer.pause()
                      this.videoPlayer.dispose(); 
                      var videoContent = document.getElementById("video-content");
                      videoContent.innerHTML = " ";            
                      var id = "myvideo"+this.videoId;
                      this.getVideo(id);
                    }else{                  
                        var id = "myvideo"+this.videoId;
                        this.getVideo(id);
                    }
                }  
          });
      });    
      var id = "myvideo"+this.videoId;
      this.getVideo(id);
    },
    methods: {
      getVideo(id){
         this.videoPlayer = videojs(id, {
                bigPlayButton: true,
                textTrackDisplay: false,
                posterImage: true,
                errorDisplay: false,
                controlBar: true
            }, function () {
                // this.play()
            })
        }
    },
    beforeDestroy:function(){
         this.videoPlayer.dispose();  
    },
  }

你可能感兴趣的:(vue+videojs+videojs-contrib-hl实现hls视频流切换播放)