使用hls.js播放m3u8視頻流



<template> <div class="video_con"> <video controls class="video" ref="video"></video> </div> </template> <script>
   // npm install hls.js -S let Hls = require('hls.js'); export default { data() { return { hls: null } }, mounted() { this.getStream('url') }, methods: { getStream(source) { if (Hls.isSupported()) { this.hls = new Hls(); this.hls.loadSource(source); this.hls.attachMedia(this.$refs.video); this.hls.on(Hls.Events.MANIFEST_PARSED, () => { console.log("加載成功"); this.$refs.video.play(); }); this.hls.on(Hls.Events.ERROR, (event, data) => { // console.log(event, data); // 監聽出錯事件 console.log("加載失敗"); }); } else if (this.$refs.video.canPlayType('application/vnd.apple.mpegurl')) { this.$refs.video.src = 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8'; this.$refs.video.addEventListener('loadedmetadata',function() { this.$refs.video.play(); }); } }, // 停止流 videoPause() { if (this.hls) { this.$refs.video.pause(); this.hls.destroy(); this.hls = null; } } } } </script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM