fix: append signed query through HLS loader

This commit is contained in:
Zhou Weidong committed 2026-11-10 17:19:18 +08:00
1 parent 15c245f8e3
commit 773a53e52e
1 file changed
+3 -1
+3 -1
View File
@@ -2,9 +2,11 @@
import { useEffect, useRef, useState } from 'react';
import Hls from 'hls.js';
function createSignedLoader(sourceUrl) { return class SignedLoader extends Hls.DefaultConfig.loader { constructor(config) { super(config); this.sourceUrl = sourceUrl; } load(context, config, callbacks) { const request = new URL(context.url, window.location.href); const source = new URL(this.sourceUrl, window.location.href); if (!request.search && source.search) request.search = source.search; context.url = request.toString(); return super.load(context, config, callbacks); } }; }
export function VideoPlayer({ src, poster, autoPlay = true, muted = true, onProgress }) {
const ref = useRef(null); const [ready, setReady] = useState(false); const [error, setError] = useState('');
useEffect(() => { const video = ref.current; if (!video || !src) return undefined; video.muted = muted; setReady(false); setError(''); const source = new URL(src, window.location.href); const hls = new Hls({ enableWorker: true, xhrSetup: (xhr, requestUrl) => { const request = new URL(requestUrl, window.location.href); if (!request.search && source.search) request.search = source.search; xhr.open('GET', request.toString(), true); } }); const start = () => { setReady(true); if (autoPlay) video.play().catch(() => {}); else video.pause(); }; const onError = (_event, data) => { if (data?.fatal) setError(data.details || 'Video stream error'); }; hls.on(Hls.Events.MANIFEST_PARSED, start); hls.on(Hls.Events.ERROR, onError); if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = src; video.addEventListener('loadedmetadata', start, { once: true }); } else if (Hls.isSupported()) { hls.loadSource(src); hls.attachMedia(video); } else setError('This browser does not support HLS'); return () => { video.removeEventListener('loadedmetadata', start); hls.destroy(); video.pause(); }; }, [src, autoPlay, muted]);
useEffect(() => { const video = ref.current; if (!video || !src) return undefined; video.muted = muted; setReady(false); setError(''); const hls = new Hls({ enableWorker: true, loader: createSignedLoader(src) }); const start = () => { setReady(true); if (autoPlay) video.play().catch(() => {}); else video.pause(); }; const onError = (_event, data) => { if (data?.fatal) setError(data.details || 'Video stream error'); }; hls.on(Hls.Events.MANIFEST_PARSED, start); hls.on(Hls.Events.ERROR, onError); if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = src; video.addEventListener('loadedmetadata', start, { once: true }); } else if (Hls.isSupported()) { hls.loadSource(src); hls.attachMedia(video); } else setError('This browser does not support HLS'); return () => { video.removeEventListener('loadedmetadata', start); hls.destroy(); video.pause(); }; }, [src, autoPlay, muted]);
const playNow = () => { const video = ref.current; if (video) { video.muted = false; video.play().catch((playError) => setError(playError.message)); } };
return <div className="video-player-wrap"><video ref={ref} className="video-player" poster={poster} controls playsInline muted={muted} onTimeUpdate={(event) => onProgress?.(event.currentTarget.currentTime)} />{!ready && !error && <button className="video-play-button" onClick={playNow}>▶</button>}{error && <button className="video-error-button" onClick={playNow}>{error} · 点击重试</button>}</div>;
}