音乐组件 (MusicCard) 技术解析
#说明文档#配置文档
2026年 2月 24日

因为音乐组件被我大改过,很多人问我具体功能和配置问题,所以就有了这篇文章

组件文件位置

  • 音乐组件:src/components/music-card.tsx
  • 音乐 API 路由:src/app/api/music/route.ts(如果你的项目原本没有这个文件要新建这个文件,或者直接fork我的项目)

组件介绍

音乐组件是一个功能完整的音乐播放器,集成在博客首页的卡片系统中。它支持音乐播放、暂停、上一曲、下一曲、播放模式切换等功能,同时提供了美观的用户界面和流畅的交互体验。

功能特性

核心功能

  • 音乐播放控制:播放/暂停、上一首/下一首
  • 多种播放模式:
    • 列表播放不循环
    • 列表循环
    • 单曲循环
    • 随机播放
  • 音乐列表:点击卡片显示完整音乐列表,支持歌曲选择
  • 进度显示:实时显示当前歌曲播放进度
  • 响应式布局:在首页可拖拽定位,在其他页面自动定位到右下角
  • 节日主题:支持圣诞节主题装饰

技术特性

  • 客户端组件:使用 'use client' 指令,确保在客户端渲染
  • 状态管理:使用 Zustand 进行全局状态管理
  • 动画效果:使用 Motion React 实现平滑的交互动画
  • 响应式设计:根据页面类型自动调整布局
  • 性能优化:使用 useMemo 缓存计算结果,减少不必要的重渲染

技术实现

组件结构

MusicCard
├── Card (基础卡片组件)
├── HomeDraggableLayer (拖拽层)
├── 音乐播放控制按钮
├── 进度条
├── 音乐列表弹窗
└── 节日装饰 (可选)

关键技术点

1. 音乐播放核心

使用 HTML5 Audio API 实现音乐播放功能:

// 初始化音频元素
useEffect(() => {
  if (!audioRef.current) {
    audioRef.current = new Audio()
  }
  
  const audio = audioRef.current
  
  // 更新进度条
  const updateProgress = () => {
    if (audio.duration) {
      setProgress((audio.currentTime / audio.duration) * 100)
    }
  }
  
  // 处理歌曲结束事件
  const handleEnded = () => {
    // 根据不同播放模式处理逻辑
    // ...
  }
  
  // 添加事件监听器
  audio.addEventListener('timeupdate', updateProgress)
  audio.addEventListener('ended', handleEnded)
  audio.addEventListener('loadedmetadata', updateProgress)
  
  // 清理函数
  return () => {
    audio.removeEventListener('timeupdate', updateProgress)
    audio.removeEventListener('ended', handleEnded)
    audio.removeEventListener('loadedmetadata', updateProgress)
  }
}, [loopMode, currentIndex, musicFiles])

2. 播放模式切换

支持四种播放模式的切换和对应逻辑:

const handleEnded = () => {
  if (musicFiles.length === 0) return
  switch (loopMode) {
  case 'single':
    // 单曲循环
    if (audioRef.current) {
      audioRef.current.currentTime = 0
      audioRef.current.play().catch(console.error)
    }
    break
  case 'list':
    // 列表循环
    const nextIndex = (currentIndex + 1) % musicFiles.length
    currentIndexRef.current = nextIndex
    setCurrentIndex(nextIndex)
    setProgress(0)
    setIsPlaying(true)
    break
  case 'shuffle':
    // 随机播放
    const randomIndex = Math.floor(Math.random() * musicFiles.length)
    currentIndexRef.current = randomIndex
    setCurrentIndex(randomIndex)
    setProgress(0)
    setIsPlaying(true)
    break
  case 'none':
  default:
    // 列表播放不循环
    if (currentIndex < musicFiles.length - 1) {
      // 还有下一首,继续播放
      const nextIndex = currentIndex + 1
      currentIndexRef.current = nextIndex
      setCurrentIndex(nextIndex)
      setProgress(0)
      setIsPlaying(true)
    } else {
      // 已经是最后一首,停止播放
      setIsPlaying(false)
      setProgress(0)
    }
    break
  }
}

3. 动态定位

根据页面类型和播放状态自动调整组件位置:

const position = useMemo(() => {
  // If not on home page, always position at bottom-right corner when playing
  if (!isHomePage) {
    return {
      x: center.width - styles.width - 16,
      y: center.height - styles.height - 16
    }
  }

  // Default position on home page
  return {
    x: styles.offsetX !== null ? center.x + styles.offsetX : center.x + CARD_SPACING + hiCardStyles.width / 2 - styles.offset,
    y: styles.offsetY !== null ? center.y + styles.offsetY : center.y - clockCardStyles.offset + CARD_SPACING + calendarCardStyles.height + CARD_SPACING
  }
}, [isPlaying, isHomePage, center, styles, hiCardStyles, clockCardStyles, calendarCardStyles])

4. 音乐列表弹窗

点击卡片显示音乐列表弹窗,支持歌曲选择:

音乐播放列表弹窗
{showPlaylist && (
  <>
    <div className="fixed inset-0 bg-black/50 z-40" onClick={togglePlaylist} />
    <div className="fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 z-50">
      <div className="bg-card/80 backdrop-blur-lg p-4 rounded-2xl shadow-xl max-h-96 overflow-y-auto w-80 border border-white/20 scrollbar-none">
        <div className="flex justify-between items-center mb-4">
          <h3 className="text-lg font-semibold text-primary">音乐列表</h3>
          <button onClick={togglePlaylist} className="text-secondary hover:text-primary">
            {showPlaylist ? <ChevronDown className="h-5 w-5" /> : <ChevronUp className="h-5 w-5" />}
          </button>
        </div>
        <div className="space-y-2">
          {musicFiles.map((song, index) => (
            <button
              key={index}
              onClick={() => handleSongSelect(index)}
              className={`w-full text-left p-3 rounded-xl transition-colors ${
                index === currentIndex 
                  ? 'bg-brand/20 text-brand font-medium'
                  : 'hover:bg-white/10 text-primary'
                }`}
              >
                <div className="font-medium">{song.title}</div>
              </button>
          ))}
        </div>
      </div>
    </div>
  </>
)}

使用方法

组件集成

在首页组件中引入并使用 MusicCard 组件:

import MusicCard from '@/components/music-card'

// 在页面组件中使用
function HomePage() {
  return (
    <>
      {/* 其他组件 */}
      <MusicCard />
    </>
  )
}

配置

组件使用 useConfigStore 获取配置信息,包括卡片样式和站点内容:

const { cardStyles, siteContent } = useConfigStore()
const styles = cardStyles.musicCard

音乐文件

音乐文件通过 API 获取,需要在 /api/music 端点提供音乐文件列表:

// 从API获取音乐文件列表
useEffect(() => {
  const fetchMusicFiles = async () => {
    try {
      const response = await fetch('/api/music')
      if (response.ok) {
        const data = await response.json()
        setMusicFiles(data)
      }
    } catch (error) {
      console.error('Failed to fetch music files:', error)
    }
  }

  fetchMusicFiles()
}, [])

常见问题与解决方案

1. 音乐不播放

可能原因:

  • 浏览器自动播放策略限制
  • 音频文件路径错误
  • 网络请求失败

解决方案:

  • 确保你和页面有交互后再触发播放
  • 检查音频文件路径是否正确
  • 添加网络错误处理和重试机制

音乐 API 路由说明

API 是什么?

对于小白来说,API(Application Programming Interface,应用程序编程接口)可以理解为不同软件组件之间的通信桥梁。在这个项目中,音乐组件通过调用 /api/music 这个 API 端点来获取音乐文件列表。

音乐 API 实现

音乐 API 路由位于 src/app/api/music/route.ts,它的主要功能是读取 public/music 目录下的音乐文件,并返回格式化的音乐文件列表。

import { readdirSync } from 'fs'
import { join } from 'path'
import { NextRequest, NextResponse } from 'next/server'

// 支持的音乐文件格式
const SUPPORTED_FORMATS = ['.mp3', '.wav', '.ogg', '.m4a', '.flac']

export async function GET(request: NextRequest) {
  try {
    // 读取 public/music 目录
    const musicDir = join(process.cwd(), 'public', 'music')
    const files = readdirSync(musicDir)

    // 过滤出支持的音乐文件并提取信息
    const musicFiles = files
      .filter(file => {
        const ext = file.toLowerCase().slice(file.lastIndexOf('.'))
        return SUPPORTED_FORMATS.includes(ext)
      })
      .map(file => {
        // 提取标题(去除文件扩展名)
        const lastDotIndex = file.lastIndexOf('.')
        const title = lastDotIndex > 0 ? file.slice(0, lastDotIndex) : file
        // 构建文件路径
        const path = `/music/${file}`
        return { path, title }
      })

    return NextResponse.json(musicFiles)
  } catch (error) {
    console.error('Error reading music files:', error)
    return NextResponse.json([], { status: 500 })
  }
}

如何使用音乐 API

1. 准备音乐文件

  1. 在项目根目录创建 public/music 文件夹
  2. 将音乐文件(支持 .mp3, .wav, .ogg, .m4a, .flac 格式)放入该文件夹
  3. 音乐文件的文件名会被用作歌曲标题(自动去除扩展名)

2. API 调用流程

  1. 音乐组件在初始化时调用 /api/music 端点
  2. API 读取 public/music 目录下的所有音乐文件
  3. API 过滤出支持的文件格式,并提取标题和路径信息
  4. API 返回格式化的音乐文件列表(JSON 格式)
  5. 音乐组件接收并使用这些数据显示音乐列表和播放音乐

常见问题与解决方案

1. API 返回空数组

可能原因:

  • public/music 目录不存在
  • 目录中没有支持格式的音乐文件
  • 目录权限问题

解决方案:

  • 确保创建了 public/music 目录
  • 放入至少一个支持格式的音乐文件
  • 检查目录权限是否正确

2. 音乐文件不显示

可能原因:

  • 文件格式不被支持
  • 文件名包含特殊字符

解决方案:

  • 确保使用支持的文件格式(.mp3, .wav, .ogg, .m4a, .flac)
  • 尽量使用简单的文件名,避免特殊字符

3. 音乐播放失败

可能原因:

  • 音乐文件路径错误
  • 音乐文件损坏
  • 浏览器安全策略限制

解决方案:

  • 检查 API 返回的文件路径是否正确
  • 尝试使用其他音乐文件测试
  • 确保用户与页面有交互后再触发播放

总结

该组件的设计思路和实现方法可以作为类似媒体播放器组件的参考,也可以通过进一步扩展,添加更多功能,如歌词显示、音量控制、音频可视化等,以满足更复杂的使用场景。(需要歌词或者音量控制的可以在我的github上提issue,因为目前我没有这个需求所以没弄)

加载中...