fix: start HLS playback after manifest loads

This commit is contained in:
Zhou Weidong committed 2026-11-10 16:32:09 +08:00
1 parent 41e9cdf2e9
commit 0161bcf81b
1 file changed
+1 -1
+1 -1
View File
@@ -4,6 +4,6 @@ import Hls from 'hls.js';
export function VideoPlayer({ src, poster, autoPlay = true, muted = true, onProgress }) {
const ref = useRef(null);
useEffect(() => { const video = ref.current; if (!video || !src) return undefined; const hls = new Hls(); if (video.canPlayType('application/vnd.apple.mpegurl')) video.src = src; else if (Hls.isSupported()) { hls.loadSource(src); hls.attachMedia(video); } if (autoPlay) video.play().catch(() => {}); else video.pause(); return () => { hls.destroy(); video.pause(); }; }, [src, autoPlay]);
useEffect(() => { const video = ref.current; if (!video || !src) return undefined; video.muted = muted; const hls = new Hls({ enableWorker: true }); const start = () => { if (autoPlay) video.play().catch(() => {}); else video.pause(); }; if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = src; video.addEventListener('loadedmetadata', start, { once: true }); } else if (Hls.isSupported()) { hls.on(Hls.Events.MANIFEST_PARSED, start); hls.loadSource(src); hls.attachMedia(video); } return () => { video.removeEventListener('loadedmetadata', start); hls.destroy(); video.pause(); }; }, [src, autoPlay, muted]);
return <video ref={ref} className="video-player" poster={poster} controls playsInline autoPlay={autoPlay} muted={muted} onTimeUpdate={(event) => onProgress?.(event.currentTarget.currentTime)} />;
}