Html播放m3u8视频

一,首先现在hls.js

hls.js

二,html代码

html 复制代码
<video id="video" webkit-playsinline playsinline x5-playsinline controls
      width="100%" height="100%"
      poster="/statics/video/poster.png">
                    您的浏览器不支持视频播放
</video>
<script src="/statics/js/hls.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const video = document.getElementById('video');

        // 视频源地址
        const videoSrc = "/statics/video/jzauntexam.m3u8";

        // 初始化 HLS 播放器
        function initHls() {
            if (Hls.isSupported()) {
                const hls = new Hls({
                    enableWorker: true,
                    lowLatencyMode: true,
                    backBufferLength: 90
                });

                hls.loadSource(videoSrc);
                hls.attachMedia(video);

                hls.on(Hls.Events.MANIFEST_PARSED, function(event, data) {
                    console.log('HLS manifest parsed', data);

                    // 尝试自动播放
                    //attemptPlay();
                });

                hls.on(Hls.Events.MEDIA_ATTACHED, function() {
                    console.log('HLS media attached');
                });

                hls.on(Hls.Events.ERROR, function(event, data) {
                    console.error('HLS error:', data);
                    handleError(data);
                });

                hls.on(Hls.Events.MEDIA_ATTACHING, function() {
                    console.log('HLS media attaching');
                });
            }
            // Safari 浏览器原生支持 HLS
            else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = videoSrc;
                video.addEventListener('loadedmetadata', function() {
                    console.log('Safari HLS loaded');

                    // 尝试自动播放
                    //attemptPlay();
                });
                video.addEventListener('error', function(e) {
                    console.error('Safari HLS error:', e);
                });
            }
            // 不支持 HLS 的情况
            else {
                console.error('HLS is not supported in this browser');
                alert('您的浏览器不支持播放该视频格式');
            }
        }

        // 尝试自动播放
        function attemptPlay() {
            const playPromise = video.play();
            if (playPromise !== undefined) {
                playPromise
                    .then(() => {
                        console.log('自动播放成功');
                    })
                    .catch(error => {
                        console.log('自动播放被阻止:', error);
                    });
            }
        }

        // 错误处理
        function handleError(error) {
            if (error.type === Hls.ErrorTypes.MEDIA_ERROR) {
                console.warn('media error encountered, try to recover');
            }
            if (error.fatal) {
                switch(error.type) {
                    case Hls.ErrorTypes.NETWORK_ERROR:
                        console.log('network error');
                        break;
                    case Hls.ErrorTypes.MEDIA_ERROR:
                        console.log('media error');
                        break;
                    default:
                        console.log('other error');
                }
            }
        }

        // 初始化 HLS
        initHls();

        // 事件监听
        video.addEventListener('waiting', () => {
        });
        video.addEventListener('playing', () => {
        });
        video.addEventListener('timeupdate', ()=>{
            //监听播放进度
            if (video.currentTime > 0 && video.currentTime >= video.duration){
                app.ksbtndisable = false;
            }
        });
    });
</script>
相关推荐
woshikejiaih13 分钟前
**播客听书与有声书区别解析2026指南,适配不同场景的音频
大数据·人工智能·python·音视频
Mr数据杨1 小时前
【ComfyUI】AV-FunASR 音频转文本
音视频
凉辰4 小时前
使用uni.createInnerAudioContext()播放指定音频(踩坑分享功能)
开发语言·javascript·音视频
AI资源库5 小时前
Remotion 一个用 React 程序化制作视频的框架
人工智能·语言模型·音视频
永远都不秃头的程序员(互关)6 小时前
基于CANN的ops-signal仓库实现AIGC音频生成中的动态窗函数融合优化——从STFT预处理到端到端低延迟合成
aigc·音视频
薛定谔的猫喵喵6 小时前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
byte轻骑兵6 小时前
从HCI报文透视LE Audio重连流程(3):音频流建立、同步与终止
音视频·蓝牙·le audio·cig/cis·广播音频
三十_A7 小时前
零基础通过 Vue 3 实现前端视频录制 —— 从原理到实战
前端·vue.js·音视频
愚公搬代码7 小时前
【愚公系列】《AI短视频创作一本通》018-AI语音及音乐的创作(短视频背景音乐的选择及创作)
人工智能·音视频
sweetone18 小时前
LINN莲CLASSIK桌面音响微修
经验分享·音视频