feat: add buffering spinner to VideoPlayer component
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { forwardRef, useEffect, useRef } from "react";
|
import { forwardRef, useEffect, useRef, useState } from "react";
|
||||||
import Hls from "hls.js";
|
import Hls from "hls.js";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
|
||||||
export interface SubtitleTrack {
|
export interface SubtitleTrack {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -34,6 +35,7 @@ const VideoPlayer = forwardRef<HTMLVideoElement, VideoPlayerProps>(
|
|||||||
) => {
|
) => {
|
||||||
const internalRef = useRef<HTMLVideoElement | null>(null);
|
const internalRef = useRef<HTMLVideoElement | null>(null);
|
||||||
const hlsRef = useRef<Hls | null>(null);
|
const hlsRef = useRef<Hls | null>(null);
|
||||||
|
const [isBuffering, setIsBuffering] = useState(true);
|
||||||
|
|
||||||
const setRef = (el: HTMLVideoElement | null) => {
|
const setRef = (el: HTMLVideoElement | null) => {
|
||||||
internalRef.current = el;
|
internalRef.current = el;
|
||||||
@@ -55,6 +57,7 @@ const VideoPlayer = forwardRef<HTMLVideoElement, VideoPlayerProps>(
|
|||||||
hlsRef.current?.destroy();
|
hlsRef.current?.destroy();
|
||||||
hlsRef.current = null;
|
hlsRef.current = null;
|
||||||
onSubtitleTracksChange?.([]);
|
onSubtitleTracksChange?.([]);
|
||||||
|
setIsBuffering(true);
|
||||||
|
|
||||||
const isHls = src.includes(".m3u8");
|
const isHls = src.includes(".m3u8");
|
||||||
|
|
||||||
@@ -117,9 +120,18 @@ const VideoPlayer = forwardRef<HTMLVideoElement, VideoPlayerProps>(
|
|||||||
ref={setRef}
|
ref={setRef}
|
||||||
poster={poster}
|
poster={poster}
|
||||||
playsInline
|
playsInline
|
||||||
|
onPlaying={() => setIsBuffering(false)}
|
||||||
|
onWaiting={() => setIsBuffering(true)}
|
||||||
onError={onStreamError}
|
onError={onStreamError}
|
||||||
className="h-full w-full object-contain"
|
className="h-full w-full object-contain"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{/* Buffering spinner — shown until frames are actually rendering */}
|
||||||
|
{isBuffering && (
|
||||||
|
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
||||||
|
<Loader2 className="h-10 w-10 animate-spin text-zinc-500" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user