搜索结果

×

搜索结果将在这里显示。

💙 高级音乐播放器 带频谱

高级音乐播放器 带频谱
注意 音乐MP3和index.html 上传在同一文件夹里面

代码如下


<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高级音乐播放器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: white;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .music-player {
            width: 90%;
            max-width: 900px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 25px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
        }

        .player-header {
            text-align: center;
            margin-bottom: 25px;
        }

        .player-header h1 {
            font-size: 28px;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        .song-info {
            margin-bottom: 20px;
        }

        .song-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .song-artist {
            font-size: 16px;
            opacity: 0.9;
        }

        .visualization-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 25px 0;
            gap: 15px;
        }

        .rotating-image {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            background-size: cover;
            background-position: center;
            border: 4px solid rgba(255, 255, 255, 0.2);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
            flex-shrink: 0;
        }

        .left-image {
            background-image: url('https://picsum.photos/seed/left/300');
        }

        .right-image {
            background-image: url('https://picsum.photos/seed/right/300');
        }

        .rotate-animation {
            animation: rotate 12s linear infinite;
            animation-play-state: paused;
        }

        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        .spectrum-container {
            flex-grow: 1;
            height: 150px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            overflow: hidden;
        }

        #spectrum {
            width: 100%;
            height: 100%;
            display: block;
        }

        .progress-container {
            width: 100%;
            height: 8px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 4px;
            overflow: hidden;
            margin: 20px 0;
            cursor: pointer;
        }

        .progress-bar {
            height: 100%;
            width: 0%;
            background: linear-gradient(90deg, #ff9966, #ff5e62);
            border-radius: 4px;
            transition: width 0.1s linear;
        }

        .time-display {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
            opacity: 0.9;
            margin-bottom: 20px;
        }

        .controls {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            margin-bottom: 20px;
        }

        .btn {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
            border: none;
            color: white;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 18px;
            transition: all 0.2s;
        }

        .btn:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(1.1);
        }

        .btn-play {
            width: 60px;
            height: 60px;
            font-size: 24px;
            background: rgba(255, 255, 255, 0.2);
        }

        .volume-control {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 15px;
        }

        #volume {
            flex-grow: 1;
            height: 5px;
            -webkit-appearance: none;
            appearance: none;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 5px;
            outline: none;
        }

        #volume::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: #ff5e62;
            cursor: pointer;
        }

        .playlist-container {
            margin-top: 25px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            padding: 15px;
            max-height: 250px;
            overflow-y: auto;
        }

        .playlist-header {
            display: flex;
            justify-content: space-between;
            padding: 10px 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            font-weight: bold;
            margin-bottom: 10px;
            position: sticky;
            top: 0;
            background: rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
        }

        .playlist {
            list-style: none;
        }

        .playlist-item {
            display: flex;
            justify-content: space-between;
            padding: 12px 15px;
            border-radius: 8px;
            margin-bottom: 8px;
            background: rgba(255, 255, 255, 0.1);
            cursor: pointer;
            transition: background 0.2s;
        }

        .playlist-item:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .playlist-item.active {
            background: rgba(255, 255, 255, 0.25);
            box-shadow: 0 0 10px rgba(255, 94, 98, 0.5);
        }

        .song-name {
            flex-grow: 1;
        }

        .song-duration {
            opacity: 0.9;
            margin-left: 15px;
            font-variant-numeric: tabular-nums;
        }

        /* 滚动条样式 */
        .playlist-container::-webkit-scrollbar {
            width: 6px;
        }

        .playlist-container::-webkit-scrollbar-track {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
        }

        .playlist-container::-webkit-scrollbar-thumb {
            background: rgba(255, 255, 255, 0.3);
            border-radius: 10px;
        }

        .playlist-container::-webkit-scrollbar-thumb:hover {
            background: rgba(255, 255, 255, 0.5);
        }

        @media (max-width: 768px) {
            .visualization-container {
                flex-direction: column;
            }

            .rotating-image {
                width: 120px;
                height: 120px;
            }

            .spectrum-container {
                width: 100%;
                margin: 15px 0;
            }
        }
    </style>
</head>
<body>
    <div class="music-player">
        <div class="player-header">
            <h1>高级音乐播放器</h1>
            <div class="song-info">
                <div class="song-title" id="songTitle">选择歌曲播放</div>
                <div class="song-artist" id="songArtist">未知艺术家</div>
            </div>
        </div>

        <div class="visualization-container">
            <div class="rotating-image left-image rotate-animation" id="leftImage"></div>

            <div class="spectrum-container">
                <canvas id="spectrum"></canvas>
            </div>

            <div class="rotating-image right-image rotate-animation" id="rightImage"></div>
        </div>

        <div class="progress-container" id="progressContainer">
            <div class="progress-bar" id="progressBar"></div>
        </div>

        <div class="time-display">
            <span id="currentTime">0:00</span>
            <span id="totalTime">0:00</span>
        </div>

        <div class="controls">
            <button class="btn" id="prevBtn">⏮</button>
            <button class="btn btn-play" id="playBtn">▶</button>
            <button class="btn" id="nextBtn">⏭</button>
        </div>

        <div class="volume-control">
            <span>🔈</span>
            <input type="range" id="volume" min="0" max="1" step="0.01" value="0.7">
            <span>🔊</span>
        </div>

        <div class="playlist-container">
            <!-- <div class="playlist-header">
                <span>歌曲列表</span>
                <span>时长</span>
            </div> -->
            <ul class="playlist" id="playlist">
                <!-- 歌曲列表将通过JavaScript动态生成 -->
            </ul>
        </div>
    </div>

    <audio id="audioPlayer"></audio>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const audio = document.getElementById('audioPlayer');
            const playBtn = document.getElementById('playBtn');
            const prevBtn = document.getElementById('prevBtn');
            const nextBtn = document.getElementById('nextBtn');
            const progressContainer = document.getElementById('progressContainer');
            const progressBar = document.getElementById('progressBar');
            const currentTimeEl = document.getElementById('currentTime');
            const totalTimeEl = document.getElementById('totalTime');
            const volumeSlider = document.getElementById('volume');
            const playlistEl = document.getElementById('playlist');
            const songTitle = document.getElementById('songTitle');
            const songArtist = document.getElementById('songArtist');
            const leftImage = document.getElementById('leftImage');
            const rightImage = document.getElementById('rightImage');
            const spectrumCanvas = document.getElementById('spectrum');
            const spectrumCtx = spectrumCanvas.getContext('2d');

            // 设置Canvas尺寸
            function setCanvasSize() {
                spectrumCanvas.width = spectrumCanvas.offsetWidth;
                spectrumCanvas.height = spectrumCanvas.offsetHeight;
            }

            setCanvasSize();
            window.addEventListener('resize', setCanvasSize);

            // 音频分析器设置
            let audioContext, analyser, source, dataArray;

            // 初始化音频分析器
            function initAudioAnalyzer() {
                audioContext = new (window.AudioContext || window.webkitAudioContext)();
                analyser = audioContext.createAnalyser();
                source = audioContext.createMediaElementSource(audio);
                source.connect(analyser);
                analyser.connect(audioContext.destination);
                analyser.fftSize = 256;
                dataArray = new Uint8Array(analyser.frequencyBinCount);
            }

            // 绘制频谱
            function drawSpectrum() {
                if (!analyser) return;

                requestAnimationFrame(drawSpectrum);

                analyser.getByteFrequencyData(dataArray);

                spectrumCtx.clearRect(0, 0, spectrumCanvas.width, spectrumCanvas.height);

                const barWidth = (spectrumCanvas.width / dataArray.length) * 2.5;
                let barHeight;
                let x = 0;

                for (let i = 0; i < dataArray.length; i++) {
                    barHeight = dataArray[i] / 255 * spectrumCanvas.height;

                    // 创建渐变颜色
                    const gradient = spectrumCtx.createLinearGradient(0, spectrumCanvas.height - barHeight, 0, spectrumCanvas.height);
                    gradient.addColorStop(0, `rgb(${Math.floor(barHeight * 3)}, 100, 200)`);
                    gradient.addColorStop(1, `rgb(50, 20, 100)`);

                    spectrumCtx.fillStyle = gradient;
                    spectrumCtx.fillRect(x, spectrumCanvas.height - barHeight, barWidth, barHeight);

                    x += barWidth + 1;
                }
            }

            // 歌曲列表数据
            const songs = [
                {
                    title: "怎会有人知你的苦",
                    artist: "bgm",
                    duration: "4:14",
                    src: "https://5blog.cn/tx/1/%E6%80%8E%E4%BC%9A%E6%9C%89%E4%BA%BA%E7%9F%A5%E4%BD%A0%E7%9A%84%E8%8B%A6%20%28DJ%E7%89%88%29.m4a",
                    cover: "https://picsum.photos/seed/song1/300"
                },
                {
                    title: "你在他乡还好吗",
                    artist: "bgm",
                    duration: "7:19",
                    src: "https://5blog.cn/tx/1/%E4%BD%A0%E5%9C%A8%E4%BB%96%E4%B9%A1%E8%BF%98%E5%A5%BD%E5%90%97.mp3",
                    cover: "https://picsum.photos/seed/song2/300"
                },
                {
                    title: "难解",
                    artist: "bgm",
                    duration: "3:06",
                    src: "https://5blog.cn/tx/1/%E9%9A%BE%E8%A7%A3.mp3",
                    cover: "https://picsum.photos/seed/song3/300"
                },
                {
                    title: "追求",
                    artist: "bgm",
                    duration: "2:45",
                    src: "https://5blog.cn/tx/1/zhuiqiu.mp3",
                    cover: "https://picsum.photos/seed/song4/300"
                },
                {
                    title: " DJ小草",
                    artist: "bgm",
                    duration: "2:13",
                    src: "https://5blog.cn/tx/1/DJ%E5%B0%8F%E8%8D%89.mp3",
                    cover: "https://picsum.photos/seed/song5/300"
                }
            ];

            let currentSongIndex = 0;
            let isPlaying = false;

            // 初始化播放列表
            function initPlaylist() {
                playlistEl.innerHTML = '';

                songs.forEach((song, index) => {
                    const li = document.createElement('li');
                    li.className = 'playlist-item';
                    if (index === currentSongIndex) {
                        li.classList.add('active');
                    }

                    li.innerHTML = `
                        <span class="song-name">${song.title} - ${song.artist}</span>
                        <span class="song-duration">${song.duration}</span>
                    `;

                    li.addEventListener('click', () => {
                        currentSongIndex = index;
                        loadSong(currentSongIndex);
                        playSong();
                    });

                    playlistEl.appendChild(li);
                });
            }

            // 加载歌曲
            function loadSong(index) {
                const song = songs[index];

                audio.src = song.src;
                songTitle.textContent = song.title;
                songArtist.textContent = song.artist;

                // 更新专辑封面
                leftImage.style.backgroundImage = `url('${song.cover}')`;
                rightImage.style.backgroundImage = `url('${song.cover}')`;

                // 更新活跃状态
                const playlistItems = document.querySelectorAll('.playlist-item');
                playlistItems.forEach((item, i) => {
                    if (i === index) {
                        item.classList.add('active');
                    } else {
                        item.classList.remove('active');
                    }
                });
            }

            // 播放歌曲
            function playSong() {
                audio.play();
                isPlaying = true;
                playBtn.textContent = '⏸';
                leftImage.style.animationPlayState = 'running';
                rightImage.style.animationPlayState = 'running';

                // 初始化音频分析器(第一次播放时)
                if (!audioContext) {
                    initAudioAnalyzer();
                    drawSpectrum();
                }
            }

            // 暂停歌曲
            function pauseSong() {
                audio.pause();
                isPlaying = false;
                playBtn.textContent = '▶';
                leftImage.style.animationPlayState = 'paused';
                rightImage.style.animationPlayState = 'paused';
            }

            // 上一首
            function prevSong() {
                currentSongIndex--;
                if (currentSongIndex < 0) {
                    currentSongIndex = songs.length - 1;
                }
                loadSong(currentSongIndex);
                if (isPlaying) playSong();
            }

            // 下一首
            function nextSong() {
                currentSongIndex++;
                if (currentSongIndex >= songs.length) {
                    currentSongIndex = 0;
                }
                loadSong(currentSongIndex);
                if (isPlaying) playSong();
            }

            // 更新进度条
            function updateProgress() {
                const { currentTime, duration } = audio;
                const progressPercent = (currentTime / duration) * 100;
                progressBar.style.width = `${progressPercent}%`;

                // 更新时间显示
                currentTimeEl.textContent = formatTime(currentTime);

                if (duration) {
                    totalTimeEl.textContent = formatTime(duration);
                }
            }

            // 格式化时间
            function formatTime(seconds) {
                if (isNaN(seconds)) return "0:00";

                const mins = Math.floor(seconds / 60);
                const secs = Math.floor(seconds % 60);
                return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
            }

            // 设置进度
            function setProgress(e) {
                const width = this.clientWidth;
                const clickX = e.offsetX;
                const duration = audio.duration;
                audio.currentTime = (clickX / width) * duration;
            }

            // 事件监听器
            playBtn.addEventListener('click', () => {
                if (isPlaying) {
                    pauseSong();
                } else {
                    playSong();
                }
            });

            prevBtn.addEventListener('click', prevSong);
            nextBtn.addEventListener('click', nextSong);

            audio.addEventListener('timeupdate', updateProgress);

            audio.addEventListener('ended', nextSong);

            progressContainer.addEventListener('click', setProgress);

            volumeSlider.addEventListener('input', () => {
                audio.volume = volumeSlider.value;
            });

            // 初始化
            initPlaylist();
            loadSong(currentSongIndex);
        });
    </script>
</body>
</html>

演示地址

https://5blog.cn/tx/1/

阅读:232
发布时间:
请先 登录 再评论