Vue使用flv.js拉流播放flv文件直播視頻

加載擴(kuò)展

這里使用yarn來代替npm進(jìn)行包的管理,因?yàn)閚pm安裝的話有一些問題(其實(shí)就是當(dāng)時(shí)npm死活安不上,才用的yarn)。不過后來查到,yarn會(huì)緩存下載過的包,并且會(huì)生成lock文件來鎖定版本,而且支持并發(fā)下載,還是有一定的優(yōu)勢(shì)的。

yarn add flv.js

安裝后查看項(xiàng)目中node_modules下是否多了flv.js這個(gè)文件夾。

import 包

import flvjs from 'flv.js'

這里我起了一個(gè)別名

播放視頻

HTML部分

<el-dialog :visible="showVideoDialog"   width="50%"  :show-close="false">
        <div id="app-container">
            <video ref="videoElement" v-model="test" id="videoElement" controls autoplay muted width="600" height="450"></video>
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelVideo()">關(guān)閉</el-button>
      </div>
    </el-dialog>

script部分

播放

//play for flv
show(){
            let video =this.$refs.videoElement//定義播放路徑
            if(flvjs.isSupported()){
              this.player = flvjs.createPlayer({
                type: result.type=='flv',
                url: 'abc.flv'
              });
            }else{
              this.$message.error('不支持的格式');
              return;
            }
            this.player.attachMediaElement(video)
            this.player.load()
            this.player.play()
}

取消播放

cancelVideo() {
      this.showVideoDialog=false;
      //銷毀對(duì)象

      if(this.player){
        this.player.pause();
        this.player.destroy();
        this.player = null;
      }
}
最后編輯于
?著作權(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ù)。

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