import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, type RefObject, } from 'react'; import type {VideoRef, ReactVideoProps, VideoMetadata} from './types'; const Video = forwardRef( ( { source, paused, muted, volume, rate, repeat, controls, showNotificationControls = false, poster, onBuffer, onLoad, onProgress, onPlaybackRateChange, onError, onReadyForDisplay, onSeek, onVolumeChange, onEnd, onPlaybackStateChanged, }, ref, ) => { const nativeRef = useRef(null); const errorHandler = useRef(onError); errorHandler.current = onError; const seek = useCallback( async (time: number, _tolerance?: number) => { if (isNaN(time)) { throw new Error('Specified time is not a number'); } if (!nativeRef.current) { console.warn('Video Component is not mounted'); return; } time = Math.max(0, Math.min(time, nativeRef.current.duration)); nativeRef.current.currentTime = time; onSeek?.({seekTime: time, currentTime: nativeRef.current.currentTime}); }, [onSeek], ); const pause = useCallback(() => { if (!nativeRef.current) { return; } nativeRef.current.pause(); }, []); const resume = useCallback(() => { if (!nativeRef.current) { return; } nativeRef.current.play(); }, []); const setVolume = useCallback((vol: number) => { if (!nativeRef.current) { return; } nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100; }, []); const getCurrentPosition = useCallback(async () => { if (!nativeRef.current) { throw new Error('Video Component is not mounted'); } return nativeRef.current.currentTime; }, []); const unsupported = useCallback(() => { throw new Error('This is unsupported on the web'); }, []); useImperativeHandle( ref, () => ({ seek, pause, resume, setVolume, getCurrentPosition, // making the video fullscreen does not work with some subtitles polyfils // so I decided to not include it. presentFullscreenPlayer: unsupported, dismissFullscreenPlayer: unsupported, setFullScreen: unsupported, save: unsupported, restoreUserInterfaceForPictureInPictureStopCompleted: unsupported, nativeHtmlRef: nativeRef, }), [ seek, pause, resume, unsupported, setVolume, getCurrentPosition, nativeRef, ], ); useEffect(() => { if (paused) { pause(); } else { resume(); } }, [paused, pause, resume]); useEffect(() => { if (volume === undefined) { return; } setVolume(volume); }, [volume, setVolume]); useEffect(() => { // Not sure about how to do this but we want to wait for nativeRef to be initialized setTimeout(() => { if (!nativeRef.current) { return; } // Set play state to the player's value (if autoplay is denied) // This is useful if our UI is in a play state but autoplay got denied so // the video is actaully in a paused state. onPlaybackStateChanged?.({isPlaying: !nativeRef.current.paused}); }, 500); }, [onPlaybackStateChanged]); useEffect(() => { if (!nativeRef.current || rate === undefined) { return; } nativeRef.current.playbackRate = rate; }, [rate]); useMediaSession(source?.metadata, nativeRef, showNotificationControls); return (