[轉(zhuǎn)]flv.js+vue 視頻播放(手動(dòng)追幀、斷開重連、多個(gè)視頻同時(shí)直播)

<script>
import flvjs from 'flv.js/dist/flv.min.js'

export default {
  name:'viewVideo',
  data() {
    return {
      src:"",  //flv格式的地址
      timerId:null,
      loadStatus:true,
      id: 'videoElement'+Math.random(),
      statusMsg:'攝像機(jī)未開啟,請(qǐng)聯(lián)系'
    };
  },
 created(){
     if(this.timerId){
      clearInterval(this.timerId)
      this.timerId=null
    }
  },
   mounted() {
      this.$nextTick(() => {
        this.setsrc();
      })
    },
  destroyed(){
    console.log("destorying video");
    if(this.timerId){
      clearInterval(this.timerId)
      this.timerId=null
    }
    if (this.flvPlayer !== null) {
        this.flvPlayer.pause();
        this.flvPlayer.destroy();
        this.flvPlayer = null;
      }
  },
  methods: {
    createVideo(){
      let that=this
     console.log(that.flvPlayer);
       if (that.flvPlayer) {
           that.flvPlayer.pause();
           that.flvPlayer.destroy();
           that.flvPlayer = null;
        }
         if (that.timerId !== null) {
         clearInterval(that.timerId);
            }
      that.videoElement = document.getElementById(that.id)
      if (flvjs.isSupported()) {
        that.flvPlayer = flvjs.createPlayer({
          type: 'flv',
          url: that.src,
          isLive: true, 
        },{
             cors: true, // 是否跨域
            enableWorker: true, // 是否多線程工作
            enableStashBuffer: false, // 是否啟用緩存
            stashInitialSize: 128, // 緩存大小(kb)  默認(rèn)384kb
            autoCleanupSourceBuffer: true // 是否自動(dòng)清理緩存
        })
      }
         that.flvPlayer.on(flvjs.Events.ERROR, (errorType, errorDetail, errorInfo) => {
            console.log("errorType:", errorType);
        console.log("errorDetail:", errorDetail);
        console.log("errorInfo:", errorInfo);
        // this.loadStatus=true
        // this.statusMsg="正在重連。。。"
        //視頻出錯(cuò)后銷毀重新創(chuàng)建
         if (that.flvPlayer) {
          that.flvPlayer.pause();
          that.flvPlayer.unload();
          that.flvPlayer.detachMediaElement();
          that.flvPlayer.destroy();
          that.flvPlayer= null;
        }
          that.createVideo();
      });
      that.flvPlayer.on("statistics_info", function (res) {
       if (that.lastDecodedFrame == 0) {
         that.lastDecodedFrame = res.decodedFrames;
         return;
       }
       if (that.lastDecodedFrame != res.decodedFrames) {
         that.lastDecodedFrame = res.decodedFrames;
       } else {
           that.lastDecodedFrame = 0;
           if (that.flvPlayer) {
             that.flvPlayer.pause();
             that.flvPlayer.unload();
             that.flvPlayer.detachMediaElement();
             that.flvPlayer.destroy();
             that.flvPlayer= null;
              that.createVideo();
         }
           
       }
     });

             that.flvPlayer.attachMediaElement(this.videoElement)
             that.flvPlayer.load()
            that.flvPlayer.play()
            if (that.timerId !== null) {
          clearInterval(that.timerId);
             }
         that.timerId = setInterval(() => {
          if (that.videoElement.buffered.length > 0) {
            const end = that.videoElement.buffered.end(0);  // 視頻結(jié)尾時(shí)間
            const current = that.videoElement.currentTime;  //  視頻當(dāng)前時(shí)間
            const diff = end - current;// 相差時(shí)間
            console.log(diff);
            const diffCritical = 4; // 這里設(shè)定了超過4秒以上就進(jìn)行跳轉(zhuǎn)
            const diffSpeedUp = 1; // 這里設(shè)置了超過1秒以上則進(jìn)行視頻加速播放
            const maxPlaybackRate = 4;// 自定義設(shè)置允許的最大播放速度
            let playbackRate = 1.0; // 播放速度
            if (diff > diffCritical) {
              console.log("相差超過4秒,進(jìn)行跳轉(zhuǎn)");
              that.videoElement.currentTime = end - 1.5;
              playbackRate = Math.max(1, Math.min(diffCritical, 16));
            } else if (diff > diffSpeedUp) {
              console.log("相差超過1秒,進(jìn)行加速");
              playbackRate = Math.max(1, Math.min(diff, maxPlaybackRate, 16))
            }
            that.videoElement.playbackRate = playbackRate;
            if (that.videoElement.paused) {
              that.flvPlayer.play()
            }
          }
        }, 1000);
    },
    setsrc(){
    if(this.url){
    this.src =this.url;
    this.createVideo();
    }
  },
},
  props: {
    url:  {
        default: null,
        required: true,
        type: String | null
      },
  },
    watch: {
      url: {
        handler(newValue) {
        if(newValue != null){
          this.loadStatus=false
          this.src=newValue
          this.$nextTick(() => {
        this.setsrc();
      })
        }
        },
        deep: true,
        immediate: true,
      }
    },
}
</script>
<template>
  <div class="hello" style="width:100%; height:100%;background-color:#000000;">
    <section  v-if="!loadStatus" style="width:100%; height:100%;">
      <video autoplay controls width="100%" height="100%" :id="id"  muted></video>
    </section>
    <div v-else
         style="width: 100%;height: 100%;display: flex;justify-content:center;align-items:center;cursor: pointer">
      <span style="color: wheat;font-size: 14px;"><i class="el-icon-warning-outline" style="margin-right: 10px"></i>{{statusMsg}}</span>
    </div>
  </div>
</template>
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

原文鏈接:https://blog.csdn.net/weixin_51890314/article/details/114262827

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容