'use client'; import { useEffect, useRef, useState } from 'react'; import { Play, Pause, Volume2, VolumeX, Maximize, Settings } from 'lucide-react'; interface IPTVChannel { id: string; name: string; url: string; logo?: string; group?: string; } interface IPTVPlayerProps { channels: IPTVChannel[]; currentChannel?: IPTVChannel; onChannelChange?: (channel: IPTVChannel) => void; } export function IPTVPlayer({ channels, currentChannel, onChannelChange }: IPTVPlayerProps) { const videoRef = useRef(null); const [isPlaying, setIsPlaying] = useState(false); const [isMuted, setIsMuted] = useState(false); const [volume, setVolume] = useState(100); const [showControls, setShowControls] = useState(true); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const controlsTimeoutRef = useRef(); useEffect(() => { const video = videoRef.current; if (!video || !currentChannel) return; setIsLoading(true); setError(null); const handleLoadStart = () => setIsLoading(true); const handleCanPlay = () => { setIsLoading(false); if (isPlaying) { video.play().catch(console.error); } }; const handleError = () => { setIsLoading(false); setError('无法加载频道,请检查网络连接或尝试其他频道'); }; video.addEventListener('loadstart', handleLoadStart); video.addEventListener('canplay', handleCanPlay); video.addEventListener('error', handleError); // 加载新频道 video.src = currentChannel.url; video.load(); return () => { video.removeEventListener('loadstart', handleLoadStart); video.removeEventListener('canplay', handleCanPlay); video.removeEventListener('error', handleError); }; }, [currentChannel, isPlaying]); const togglePlay = () => { const video = videoRef.current; if (!video) return; if (isPlaying) { video.pause(); } else { video.play().catch(console.error); } setIsPlaying(!isPlaying); }; const toggleMute = () => { const video = videoRef.current; if (!video) return; video.muted = !isMuted; setIsMuted(!isMuted); }; const handleVolumeChange = (newVolume: number) => { const video = videoRef.current; if (!video) return; video.volume = newVolume / 100; setVolume(newVolume); setIsMuted(newVolume === 0); }; const toggleFullscreen = () => { const video = videoRef.current; if (!video) return; if (document.fullscreenElement) { document.exitFullscreen(); } else { video.requestFullscreen().catch(console.error); } }; const resetControlsTimeout = () => { if (controlsTimeoutRef.current) { clearTimeout(controlsTimeoutRef.current); } setShowControls(true); controlsTimeoutRef.current = setTimeout(() => { setShowControls(false); }, 3000); }; const groupedChannels = channels.reduce((acc, channel) => { const group = channel.group || '其他'; if (!acc[group]) acc[group] = []; acc[group].push(channel); return acc; }, {} as Record); return (
{/* 视频播放器 */}
); } export default IPTVPlayer;