feat hls错误自动恢复

This commit is contained in:
Ironsp
2026-07-17 14:57:25 +08:00
parent 3976b854d1
commit 98a30948ec
3 changed files with 29 additions and 5 deletions

View File

@@ -3,7 +3,8 @@ import { log } from 'node:console';
let hlsInstance: Hls | null = null;
// let hlsRetryCount = 0;
export const initHls = (monitorVideoUrl, monitorVideoRef) => {
export const initHls = (monitorVideoUrl, monitorVideoRef, isDialogVisible?: () => boolean) => {
console.log('isDialogVisible', isDialogVisible);
const url = monitorVideoUrl.value;
const video = monitorVideoRef.value;
if (!url || !video) return;
@@ -39,6 +40,26 @@ export const initHls = (monitorVideoUrl, monitorVideoRef) => {
// 加载成功,重置重试计数
// hlsRetryCount = 0;
});
// ★ 错误自动恢复hls.js 拿到空 m3u8 或 ts 加载失败时自动重试
hls.on(Hls.Events.ERROR, (event, data) => {
if (data.fatal && (!isDialogVisible || isDialogVisible())) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls.startLoad();
ElMessage.error('网络错误, 请检查网络或重试');
break;
case Hls.ErrorTypes.MEDIA_ERROR:
// 媒体解码错误 → 尝试恢复
hls.recoverMediaError();
break;
default:
// 其他致命错误 → 销毁,让下一次 initHls 重建
hls.destroy();
hlsInstance = null;
break;
}
}
});
hlsInstance = hls;
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// Safari/原生支持 HLS