"use client" import type React from "react" import { useState, useRef, useEffect } from "react" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Badge } from "@/components/ui/badge" import { Eye, Edit, Play, Pause, Volume2, VolumeX } from "lucide-react" import { Slider } from "@/components/ui/slider" export type Song = { id: string name: string composer: string lyricist: string duration: string releaseDate?: string status: string image?: string audioUrl?: string description?: string rarity?: string cardType?: string genre?: string lyrics?: string bpm?: string | number } type SongDetailDialogProps = { song: Song onEdit?: () => void } export function SongDetailDialog({ song, onEdit }: SongDetailDialogProps) { const [open, setOpen] = useState(false) const [isPlaying, setIsPlaying] = useState(false) const [isLoading, setIsLoading] = useState(false) const [volume, setVolume] = useState(80) const [isMuted, setIsMuted] = useState(false) const audioRef = useRef(null) // 初始化音频播放器 useEffect(() => { if (open && !audioRef.current && song.audioUrl) { audioRef.current = new Audio(song.audioUrl) const audio = audioRef.current // 设置事件监听器 audio.addEventListener('canplay', () => { setIsLoading(false) }) audio.addEventListener('ended', () => { setIsPlaying(false) }) audio.addEventListener('error', () => { setIsPlaying(false) setIsLoading(false) }) // 设置音量 audio.volume = volume / 100 audio.muted = isMuted } // 关闭对话框时清理 if (!open && audioRef.current) { const audio = audioRef.current audio.pause() audio.src = '' audioRef.current = null setIsPlaying(false) } return () => { if (audioRef.current) { const audio = audioRef.current audio.pause() audio.src = '' audio.removeEventListener('canplay', () => {}) audio.removeEventListener('ended', () => {}) audio.removeEventListener('error', () => {}) } } }, [open, song.audioUrl, volume, isMuted]) // 监听音量变化 useEffect(() => { if (audioRef.current) { audioRef.current.volume = volume / 100 } }, [volume]) // 监听静音状态 useEffect(() => { if (audioRef.current) { audioRef.current.muted = isMuted } }, [isMuted]) const togglePlay = (e: React.MouseEvent) => { e.stopPropagation() if (!song.audioUrl) return if (isPlaying) { audioRef.current?.pause() setIsPlaying(false) } else { setIsLoading(true) if (audioRef.current) { audioRef.current.play() .then(() => { setIsPlaying(true) setIsLoading(false) }) .catch((error) => { console.error('播放失败:', error) setIsLoading(false) }) } } } // 切换静音状态 const toggleMute = () => { setIsMuted(!isMuted) } // 处理音量变化 const handleVolumeChange = (values: number[]) => { const newVolume = values[0] setVolume(newVolume) // 如果音量从0调高,则取消静音 if (newVolume > 0 && isMuted) { setIsMuted(false) } // 如果音量调到0,则设置为静音 else if (newVolume === 0 && !isMuted) { setIsMuted(true) } } return ( 歌曲详情 查看歌曲的详细信息
{song.name} {song.audioUrl && (
{isLoading ? (
) : isPlaying ? ( ) : ( )}
)}

{song.name}

ID: {song.id}

{song.status} {song.rarity && {song.rarity}} {song.cardType && {song.cardType}}
{/* 音频控制器 - 仅当歌曲有音频URL且正在播放时显示 */} {song.audioUrl && isPlaying && (
)}

作曲

{song.composer}

作词

{song.lyricist}

时长

{song.duration}

发布日期

{song.releaseDate || "未发布"}

{song.genre && (

风格

{song.genre}

)} {song.description && (

描述

{song.description}

)} {song.lyrics && (

歌词

{song.lyrics}
)}
{song.status !== "已发布" && onEdit && ( )}
) }