'use client' import { useState, useRef, useEffect } from 'react' interface AudioPlayerProps { songId: string songTitle: string artistName: string coverUrl?: string audioUrl: string onPlayCountIncrement?: () => void } export function AudioPlayer({ songId, songTitle, artistName, coverUrl, audioUrl, onPlayCountIncrement }: AudioPlayerProps) { const [isPlaying, setIsPlaying] = useState(false) const [currentTime, setCurrentTime] = useState(0) const [duration, setDuration] = useState(0) const [volume, setVolume] = useState(0.8) const audioRef = useRef(null) useEffect(() => { if (audioRef.current) { audioRef.current.volume = volume } }, [volume]) useEffect(() => { const audio = audioRef.current if (!audio) return const updateTime = () => setCurrentTime(audio.currentTime) const updateDuration = () => setDuration(audio.duration) audio.addEventListener('timeupdate', updateTime) audio.addEventListener('loadedmetadata', updateDuration) return () => { audio.removeEventListener('timeupdate', updateTime) audio.removeEventListener('loadedmetadata', updateDuration) } }, []) const togglePlay = () => { if (audioRef.current) { if (isPlaying) { audioRef.current.pause() } else { audioRef.current.play() if (onPlayCountIncrement && currentTime === 0) { onPlayCountIncrement() } } setIsPlaying(!isPlaying) } } const handleSeek = (e: React.ChangeEvent) => { const newTime = parseFloat(e.target.value) setCurrentTime(newTime) if (audioRef.current) { audioRef.current.currentTime = newTime } } const handleVolumeChange = (e: React.ChangeEvent) => { setVolume(parseFloat(e.target.value)) } const formatTime = (time: number) => { const minutes = Math.floor(time / 60) const seconds = Math.floor(time % 60) return `${minutes}:${seconds.toString().padStart(2, '0')}` } return (
) }