1. 程式人生 > 實用技巧 >基於vue的實時視訊流開發

基於vue的實時視訊流開發

背景:多個實時視訊的介入

技術:hls.js的流媒體,支援格式已m3u8為主

解決了什麼:多個實時視訊長時間播放會有卡頓的情況

具體程式碼實現:

import Hls from 'hls.js'

  playVideo(id) {

      let  hls = new Hls();
      const _this = this
      let video = $("#camera-video")[0]
        if(Hls.isSupported()) {//檢視瀏覽器是否支援
          hls.loadSource(this.videoSrc);//輸入視訊源
          hls.attachMedia(video);//新增到視訊標籤裡
          hls.on(Hls.Events.MANIFEST_PARSED,function() {
            video.play();
          });
      this.hlsObj=hls;  }else if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = this.videoSrc; video.addEventListener('loadedmetadata',function() { video.play(); }); } },
銷燬視訊
destoryVideo() {
      this.$refs.cameraVideo.pause();
      this.hlsObj.destroy();
      this.hlsObj = null;
    }