'use client'; import { useState, useEffect, useRef } from 'react'; import { Volume2, VolumeX, Play, Pause } from 'lucide-react'; interface Props {} export default function BackgroundMusicPlayer({}: Props) { const audioRef = useRef(null); const [isPlaying, setIsPlaying] = useState(false); const [volume, setVolume] = useState(0.5); const [isLoaded, setIsLoaded] = useState(false); const [error, setError] = useState(null); useEffect(() => { const audio = audioRef.current; if (!audio) return; audio.volume = volume; }, [volume]); useEffect(() => { const audio = audioRef.current; if (!audio) return; const handleCanPlay = () => { setIsLoaded(true); setError(null); }; const handleError = () => { setError('Failed to load audio'); setIsPlaying(false); }; const handleEnded = () => { audio.currentTime = 0; audio.play().catch(() => { setIsPlaying(false); }); }; audio.addEventListener('canplay', handleCanPlay); audio.addEventListener('error', handleError); audio.addEventListener('ended', handleEnded); return () => { audio.removeEventListener('canplay', handleCanPlay); audio.removeEventListener('error', handleError); audio.removeEventListener('ended', handleEnded); }; }, []); const togglePlayPause = async () => { const audio = audioRef.current; if (!audio) return; try { if (isPlaying) { audio.pause(); setIsPlaying(false); } else { await audio.play(); setIsPlaying(true); } } catch (err) { setError('Autoplay policy prevented playback'); setIsPlaying(false); } }; const handleVolumeChange = (e: React.ChangeEvent) => { const newVolume = parseFloat(e.target.value); setVolume(newVolume); }; return (
); }