Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Create a custom HTML audio player with playlist functionality and interactive controls…

  1. User

    Create a custom HTML audio player with playlist functionality and interactive controls using HTML, CSS, and JavaScript.

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Custom HTML Audio Player with Playlist</title>
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }
    
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                min-height: 100vh;
                display: flex;
                justify-content: center;
                align-items: center;
                color: #fff;
            }
    
            .player {
                background: rgba(255, 255, 255, 0.1);
                backdrop-filter: blur(10px);
                border-radius: 20px;
                box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
                width: 90%;
                max-width: 800px;
                max-height: 90vh;
                overflow: hidden;
                display: flex;
                flex-direction: column;
            }
    
            .playlist {
                flex: 1;
                max-height: 400px;
                overflow-y: auto;
                padding: 20px;
            }
    
            .playlist h3 {
                margin-bottom: 15px;
                font-size: 1.2em;
                opacity: 0.9;
            }
    
            #playlist-list {
                list-style: none;
            }
    
            #playlist-list li {
                padding: 12px 15px;
                margin-bottom: 8px;
                background: rgba(255, 255, 255, 0.1);
                border-radius: 10px;
                cursor: pointer;
                transition: all 0.3s ease;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
    
            #playlist-list li:hover,
            #playlist-list li.active {
                background: rgba(255, 255, 255, 0.2);
                transform: translateX(5px);
            }
    
            #playlist-list li span {
                flex: 1;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
    
            .controls {
                padding: 30px;
                background: rgba(0, 0, 0, 0.2);
                border-top: 1px solid rgba(255, 255, 255, 0.1);
            }
    
            .track-info {
                text-align: center;
                margin-bottom: 20px;
            }
    
            #current-title {
                display: block;
                font-size: 1.5em;
                font-weight: bold;
                margin-bottom: 5px;
            }
    
            #current-artist {
                opacity: 0.8;
                font-size: 1em;
            }
    
            .progress {
                display: flex;
                align-items: center;
                margin-bottom: 25px;
                gap: 10px;
            }
    
            #current-time,
            #duration {
                font-size: 0.9em;
                min-width: 40px;
                opacity: 0.9;
            }
    
            #progress-bar {
                flex: 1;
                height: 6px;
                background: rgba(255, 255, 255, 0.3);
                border-radius: 3px;
                outline: none;
                cursor: pointer;
                transition: background 0.3s ease;
            }
    
            #progress-bar::-webkit-slider-thumb,
            #volume-slider::-webkit-slider-thumb {
                appearance: none;
                width: 16px;
                height: 16px;
                background: #fff;
                border-radius: 50%;
                cursor: pointer;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
            }
    
            #progress-bar:hover {
                background: rgba(255, 255, 255, 0.5);
            }
    
            .buttons {
                display: flex;
                justify-content: center;
                align-items: center;
                gap: 20px;
                margin-bottom: 20px;
            }
    
            .buttons button {
                background: rgba(255, 255, 255, 0.2);
                border: none;
                width: 50px;
                height: 50px;
                border-radius: 50%;
                color: #fff;
                font-size: 1.2em;
                cursor: pointer;
                transition: all 0.3s ease;
                display: flex;
                align-items: center;
                justify-content: center;
            }
    
            .buttons button:hover {
                background: rgba(255, 255, 255, 0.3);
                transform: scale(1.1);
            }
    
            #play-pause {
                width: 60px;
                height: 60px;
                font-size: 1.5em;
            }
    
            .volume {
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 10px;
            }
    
            #volume-slider {
                flex: 1;
                max-width: 150px;
                height: 5px;
                background: rgba(255, 255, 255, 0.3);
                border-radius: 3px;
                outline: none;
            }
    
            #volume-slider:hover {
                background: rgba(255, 255, 255, 0.5);
            }
    
            @media (max-width: 600px) {
                .player {
                    width: 95%;
                    border-radius: 15px;
                }
    
                .playlist {
                    max-height: 300px;
                    padding: 15px;
                }
    
                .controls {
                    padding: 20px;
                }
    
                #current-title {
                    font-size: 1.3em;
                }
    
                .buttons {
                    gap: 15px;
                }
    
                .buttons button {
                    width: 45px;
                    height: 45px;
                }
    
                #play-pause {
                    width: 55px;
                    height: 55px;
                }
            }
    
            /* Scrollbar styling */
            .playlist::-webkit-scrollbar {
                width: 6px;
            }
    
            .playlist::-webkit-scrollbar-track {
                background: rgba(255, 255, 255, 0.1);
                border-radius: 3px;
            }
    
            .playlist::-webkit-scrollbar-thumb {
                background: rgba(255, 255, 255, 0.3);
                border-radius: 3px;
            }
    
            .playlist::-webkit-scrollbar-thumb:hover {
                background: rgba(255, 255, 255, 0.5);
            }
        </style>
    </head>
    <body>
        <div class="player">
            <div class="playlist">
                <h3>Playlist</h3>
                <ul id="playlist-list"></ul>
            </div>
            <div class="controls">
                <div class="track-info">
                    <span id="current-title">Select a track</span>
                    <span id="current-artist">&nbsp;</span>
                </div>
                <div class="progress">
                    <span id="current-time">0:00</span>
                    <input type="range" id="progress-bar" min="0" max="100" value="0">
                    <span id="duration">0:00</span>
                </div>
                <div class="buttons">
                    <button id="prev" title="Previous">⏮</button>
                    <button id="play-pause" title="Play/Pause">▶</button>
                    <button id="next" title="Next">⏭</button>
                </div>
                <div class="volume">
                    <span>🔊</span>
                    <input type="range" id="volume-slider" min="0" max="100" value="100">
                </div>
            </div>
        </div>
    
        <audio id="audio" preload="metadata"></audio>
    
        <script>
            const audio = document.getElementById('audio');
            const playlistList = document.getElementById('playlist-list');
            const currentTitle = document.getElementById('current-title');
            const currentArtist = document.getElementById('current-artist');
            const currentTimeEl = document.getElementById('current-time');
            const durationEl = document.getElementById('duration');
            const progressBar = document.getElementById('progress-bar');
            const playPauseBtn = document.getElementById('play-pause');
            const prevBtn = document.getElementById('prev');
            const nextBtn = document.getElementById('next');
            const volumeSlider = document.getElementById('volume-slider');
    
            const playlist = [
                {
                    title: 'Happy Rock',
                    artist: 'Bensound',
                    src: 'https://www.bensound.com/bensound-music/bensound-happyrock.mp3'
                },
                {
                    title: 'Sunny',
                    artist: 'Bensound',
                    src: 'https://www.bensound.com/bensound-music/bensound-sunny.mp3'
                },
                {
                    title: 'Acoustic Breeze',
                    artist: 'Bensound',
                    src: 'https://www.bensound.com/bensound-music/bensound-acousticbreeze.mp3'
                },
                {
                    title: 'Better Days',
                    artist: 'Bensound',
                    src: 'https://www.bensound.com/bensound-music/bensound-betterdays.mp3'
                },
                {
                    title: 'Dreams',
                    artist: 'Bensound',
                    src: 'https://www.bensound.com/bensound-music/bensound-dreams.mp3'
                }
            ];
    
            let currentIndex = 0;
            let isPlaying = false;
    
            // Utility function to format time
            function formatTime(seconds) {
                const mins = Math.floor(seconds / 60);
                const secs = Math.floor(seconds % 60);
                return `${mins}:${secs.toString().padStart(2, '0')}`;
            }
    
            // Load playlist UI
            function loadPlaylist() {
                playlistList.innerHTML = '';
                playlist.forEach((track, index) => {
                    const li = document.createElement('li');
                    li.innerHTML = `
                        <span>${track.title}</span>
                        <small>${track.artist}</small>
                    `;
                    li.addEventListener('click', () => setCurrentTrack(index));
                    playlistList.appendChild(li);
                });
            }
    
            // Set current track
            function setCurrentTrack(index) {
                if (index < 0 || index >= playlist.length) return;
                currentIndex = index;
                const track = playlist[currentIndex];
                currentTitle.textContent = track.title;
                currentArtist.textContent = track.artist;
                audio.src = track.src;
                audio.load();
                updatePlaylistUI();
                if (isPlaying) {
                    audio.play().catch(e => console.error('Play failed:', e));
                }
            }
    
            // Update playlist active item
            function updatePlaylistUI() {
                const items = playlistList.querySelectorAll('li');
                items.forEach((item, i) => {
                    item.classList.toggle('active', i === currentIndex);
                });
            }
    
            // Play/Pause toggle
            function togglePlayPause() {
                if (isPlaying) {
                    audio.pause();
                } else {
                    audio.play().catch(e => console.error('Play failed:', e));
                }
            }
    
            // Next track
            function nextTrack() {
                setCurrentTrack((currentIndex + 1) % playlist.length);
            }
    
            // Previous track
            function prevTrack() {
                setCurrentTrack((currentIndex - 1 + playlist.length) % playlist.length);
            }
    
            // Update progress
            function updateProgress() {
                if (audio.duration) {
                    const progress = (audio.currentTime / audio.duration) * 100;
                    progressBar.value = progress;
                    currentTimeEl.textContent = formatTime(audio.currentTime);
                    durationEl.textContent = formatTime(audio.duration);
                }
            }
    
            // Event listeners
            audio.addEventListener('loadedmetadata', () => {
                durationEl.textContent = formatTime(audio.duration);
            });
    
            audio.addEventListener('timeupdate', updateProgress);
    
            audio.addEventListener('ended', nextTrack);
    
            audio.addEventListener('play', () => {
                isPlaying = true;
                playPauseBtn.textContent = '⏸';
            });
    
            audio.addEventListener('pause', () => {
                isPlaying = false;
                playPauseBtn.textContent = '▶';
            });
    
            progressBar.addEventListener('input', () => {
                if (audio.duration) {
                    audio.currentTime = (progressBar.value / 100) * audio.duration;
                }
            });
    
            volumeSlider.addEventListener('input', () => {
                audio.volume = volumeSlider.value / 100;
            });
    
            playPauseBtn.addEventListener('click', togglePlayPause);
            nextBtn.addEventListener('click', nextTrack);
            prevBtn.addEventListener('click', prevTrack);
    
            // Initialize
            loadPlaylist();
            setCurrentTrack(0);
            updatePlaylistUI();
        </script>
    </body>
    </html>
    

    Features Included:

    • Playlist: Clickable list of tracks (5 demo tracks from Bensound - free for non-commercial use with attribution).
    • Interactive Controls:
      • Play/Pause (▶ / ⏸)
      • Next/Previous (⏭ / ⏮) with wrap-around
      • Progress bar (seek by click/drag, auto-updates)
      • Volume slider
      • Current time / duration display
    • Visual Feedback: Active track highlighted, hover effects, smooth transitions.
    • Responsive Design: Works on desktop and mobile.
    • Self-Contained: Single HTML file, no external dependencies.
    • Modern UI: Glassmorphism effect, gradients, custom scrollbars.

    Note: Audio files are loaded from Bensound (public URLs). For production, replace src with your own files to avoid CORS/external dependency issues. Autoplay is blocked by browsers; user must interact first. Credit: Music by Bensound.com.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.