fix: support HLS playback in web browsers
This commit is contained in:
1 parent
0b8fe9ad9b
commit
2bead5327c
3 files changed
+25
-5
No files matched your search
@@ -1,8 +1,9 @@
|
||||
// Author: fruit.com.cn
|
||||
import { useEffect, useRef } from 'react';
|
||||
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 attempt = video.play(); attempt?.catch(() => {}); return () => video.pause(); }, [src]);
|
||||
return <video ref={ref} className="video-player" src={src} poster={poster} controls playsInline autoPlay={autoPlay} muted={muted} onTimeUpdate={(event) => onProgress?.(event.currentTarget.currentTime)} />;
|
||||
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); } const attempt = video.play(); attempt?.catch(() => {}); return () => { hls.destroy(); video.pause(); }; }, [src]);
|
||||
return <video ref={ref} className="video-player" poster={poster} controls playsInline autoPlay={autoPlay} muted={muted} onTimeUpdate={(event) => onProgress?.(event.currentTarget.currentTime)} />;
|
||||
}
|
||||
Reference in new issue
Block a user