import { useEffect, useState, useRef } from 'react'; import { formatTime } from '@/lib/timeUtils'; import { AUDIO_POSTER_URL } from '@/assets/audioPosterUrl'; import '../styles/IOSVideoPlayer.css'; interface IOSVideoPlayerProps { videoRef: React.RefObject; currentTime: number; duration: number; } const IOSVideoPlayer = ({ videoRef, currentTime, duration }: IOSVideoPlayerProps) => { const [videoUrl, setVideoUrl] = useState(''); const [iosVideoRef, setIosVideoRef] = useState(null); const [posterImage, setPosterImage] = useState(undefined); // Refs for hold-to-continue functionality const incrementIntervalRef = useRef(null); const decrementIntervalRef = useRef(null); // Clean up intervals on unmount useEffect(() => { return () => { if (incrementIntervalRef.current) clearInterval(incrementIntervalRef.current); if (decrementIntervalRef.current) clearInterval(decrementIntervalRef.current); }; }, []); // Get the video source URL from the main player useEffect(() => { let url = ''; if (videoRef.current && videoRef.current.querySelector('source')) { const source = videoRef.current.querySelector('source') as HTMLSourceElement; if (source && source.src) { url = source.src; } } else { // Fallback to sample video if needed url = '/videos/sample-video.mp4'; } setVideoUrl(url); // Check if the media is an audio file and set poster image const isAudioFile = url.match(/\.(mp3|wav|ogg|m4a|aac|flac)$/i) !== null; // Get posterUrl from MEDIA_DATA, or use audio-poster.jpg as fallback for audio files when posterUrl is empty, null, or "None" const mediaPosterUrl = (typeof window !== 'undefined' && (window as any).MEDIA_DATA?.posterUrl) || ''; const isValidPoster = mediaPosterUrl && mediaPosterUrl !== 'None' && mediaPosterUrl.trim() !== ''; setPosterImage(isValidPoster ? mediaPosterUrl : (isAudioFile ? AUDIO_POSTER_URL : undefined)); }, [videoRef]); // Function to jump 15 seconds backward const jumpBackward15 = () => { if (iosVideoRef) { const newTime = Math.max(0, iosVideoRef.currentTime - 15); iosVideoRef.currentTime = newTime; } }; // Function to jump 15 seconds forward const jumpForward15 = () => { if (iosVideoRef) { const newTime = Math.min(iosVideoRef.duration, iosVideoRef.currentTime + 15); iosVideoRef.currentTime = newTime; } }; // Start continuous 50ms increment when button is held const startIncrement = (e: React.MouseEvent | React.TouchEvent) => { // Prevent default to avoid text selection e.preventDefault(); if (!iosVideoRef) return; if (incrementIntervalRef.current) clearInterval(incrementIntervalRef.current); // First immediate adjustment iosVideoRef.currentTime = Math.min(iosVideoRef.duration, iosVideoRef.currentTime + 0.05); // Setup continuous adjustment incrementIntervalRef.current = setInterval(() => { if (iosVideoRef) { iosVideoRef.currentTime = Math.min(iosVideoRef.duration, iosVideoRef.currentTime + 0.05); } }, 100); }; // Stop continuous increment const stopIncrement = () => { if (incrementIntervalRef.current) { clearInterval(incrementIntervalRef.current); incrementIntervalRef.current = null; } }; // Start continuous 50ms decrement when button is held const startDecrement = (e: React.MouseEvent | React.TouchEvent) => { // Prevent default to avoid text selection e.preventDefault(); if (!iosVideoRef) return; if (decrementIntervalRef.current) clearInterval(decrementIntervalRef.current); // First immediate adjustment iosVideoRef.currentTime = Math.max(0, iosVideoRef.currentTime - 0.05); // Setup continuous adjustment decrementIntervalRef.current = setInterval(() => { if (iosVideoRef) { iosVideoRef.currentTime = Math.max(0, iosVideoRef.currentTime - 0.05); } }, 100); }; // Stop continuous decrement const stopDecrement = () => { if (decrementIntervalRef.current) { clearInterval(decrementIntervalRef.current); decrementIntervalRef.current = null; } }; return (
{/* Current Time / Duration Display */}
{formatTime(currentTime)} / {formatTime(duration)}
{/* iOS-optimized Video Element with Native Controls */} {/* iOS Video Skip Controls */}
{/* iOS Fine Control Buttons */}

This player uses native iOS controls for better compatibility with iOS devices.

); }; export default IOSVideoPlayer;