Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1· openaiHugging 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

    Simplifying audio functionality

    I'm considering a simplified approach for the audio features. For volume control, I can use a range input element. The playlist can be structured with an unordered list using data attributes for each song title. I’ll create an array of song objects that includes the title, artist, source, and cover image.

    When the page loads, I'll ensure the first track plays automatically. For playlist rendering, I’m deciding between using JavaScript to create it dynamically or sticking with static HTML. For simplicity, I'll skip displaying song durations in the playlist.Crafting audio player functionalities

    I'm laying out the functionalities for the audio player. To load a track, I’ll set the audio source and update the UI, highlighting the active playlist item. I'll add event listeners for the play/pause button and track progress update.

    It’s also important that the volume slider works as expected. While keyboard support would be nice, it seems unnecessary since the user hasn't asked for it.

    I should create a simple, styled player with CSS for the audio container, buttons, and the playlist, while including helpful comments in the JavaScript code. I’ll aim for a complete example in one HTML file featuring CSS and JavaScript.Designing the audio player interface

    I’m working on the HTML structure for the audio player. I need to include essential meta tags in the head section and utilize HTML entities for the control buttons, like play/pause and previous/next track.

    Using buttons with appropriate aria-labels is a good idea for accessibility. I’ll create buttons for previous, play/pause, and next track, and I also want a volume slider.

    For the time display, I’ll show the current and duration timestamps. I’ll ensure that as metadata loads, the progress bar updates correctly and allows the user to drag it for adjusting the playback position.Implementing audio player interactions

    I want to ensure a smooth experience when the audio's time updates the slider while the user is dragging it. To do this, I’m using a boolean variable, isSeeking. I’ll set it to true when the user interacts with the progress slider and back to false once they've dropped it.

    I'll also set the default audio volume to 0.8. For the playlist, I’ll create an unordered list in HTML, allowing for a dynamic list from the tracks array, which is preferable for demonstration purposes. Each track will include a click event that loads and plays the selected track, while also maintaining the current track index. To enhance user experience, I'll highlight the active playlist item with CSS. Also, I’ll set the audio preload to "metadata" in the HTML.Creating the final code output

    I’m focusing on producing a complete code snippet that integrates HTML, CSS, and JavaScript. The user’s request to "create... using HTML, CSS, JS" implies they want a comprehensive code example. I’ll make sure everything is well-indented for clarity.

    While the user didn’t specifically request an explanation, I think it might be helpful to include a brief “How to use” note. I should keep it straightforward without referencing any internal instructions while crafting the code. Let's put this all together! Below is a complete, self‑contained example of a custom HTML audio player with playlist, play/pause, next/prev, seek bar, time display, and volume control.

    Replace the sample audio file paths (audio/song1.mp3, etc.) with your own.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Custom Audio Player with Playlist</title>
      <style>
        :root {
          --bg: #141414;
          --fg: #ffffff;
          --accent: #1db954;
          --accent-dark: #15913f;
          --muted: #888;
          --border: #2b2b2b;
          --radius: 10px;
          --shadow: 0 10px 25px rgba(0,0,0,0.5);
        }
    
        * {
          box-sizing: border-box;
        }
    
        body {
          margin: 0;
          min-height: 100vh;
          display: flex;
          align-items: center;
          justify-content: center;
          font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
          background: radial-gradient(circle at top, #232323, #050505 60%);
          color: var(--fg);
        }
    
        .audio-player {
          width: 380px;
          max-width: 90vw;
          background: #1b1b1b;
          border-radius: var(--radius);
          box-shadow: var(--shadow);
          padding: 18px 20px 16px;
          display: flex;
          flex-direction: column;
          gap: 14px;
          border: 1px solid var(--border);
        }
    
        .track-info {
          display: flex;
          gap: 12px;
          align-items: center;
        }
    
        .cover {
          width: 60px;
          height: 60px;
          border-radius: 8px;
          background: linear-gradient(135deg, #353535, #121212);
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 22px;
        }
    
        .meta {
          flex: 1;
          overflow: hidden;
        }
    
        .title {
          font-size: 16px;
          font-weight: 600;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
    
        .artist {
          font-size: 13px;
          color: var(--muted);
          margin-top: 3px;
        }
    
        .controls-row {
          display: flex;
          align-items: center;
          gap: 10px;
        }
    
        .main-controls {
          display: flex;
          align-items: center;
          gap: 8px;
        }
    
        .control-btn {
          width: 34px;
          height: 34px;
          border-radius: 50%;
          border: none;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          font-size: 18px;
          color: var(--fg);
          background: transparent;
          transition: background 0.15s ease, transform 0.1s ease, color 0.15s ease;
        }
    
        .control-btn:focus-visible {
          outline: 2px solid var(--accent);
          outline-offset: 2px;
        }
    
        .control-btn:hover {
          background: #2a2a2a;
        }
    
        .control-btn.play {
          width: 42px;
          height: 42px;
          font-size: 20px;
          background: var(--accent);
          color: #000;
        }
    
        .control-btn.play:hover {
          background: var(--accent-dark);
          transform: translateY(-1px);
        }
    
        .time-row {
          display: flex;
          align-items: center;
          gap: 8px;
          width: 100%;
        }
    
        .time-row span {
          font-size: 11px;
          color: var(--muted);
          width: 40px;
          text-align: center;
        }
    
        .slider {
          -webkit-appearance: none;
          appearance: none;
          background: #2a2a2a;
          border-radius: 999px;
          height: 5px;
          outline: none;
          cursor: pointer;
        }
    
        .slider::-webkit-slider-thumb {
          -webkit-appearance: none;
          appearance: none;
          width: 13px;
          height: 13px;
          background: var(--accent);
          border-radius: 50%;
          border: none;
          margin-top: -4px; /* center on track */
          box-shadow: 0 0 0 4px rgba(29,185,84,0.25);
        }
    
        .slider::-moz-range-thumb {
          width: 13px;
          height: 13px;
          background: var(--accent);
          border-radius: 50%;
          border: none;
          box-shadow: 0 0 0 4px rgba(29,185,84,0.25);
        }
    
        .slider::-moz-range-track {
          background: #2a2a2a;
          border-radius: 999px;
          height: 5px;
        }
    
        #progress {
          flex: 1;
        }
    
        .volume-control {
          margin-left: auto;
          display: flex;
          align-items: center;
          gap: 4px;
          min-width: 0;
        }
    
        .volume-icon {
          font-size: 16px;
          color: var(--muted);
        }
    
        #volume {
          width: 80px;
        }
    
        .playlist {
          border-top: 1px solid var(--border);
          margin-top: 4px;
          padding-top: 8px;
          max-height: 190px;
          overflow-y: auto;
        }
    
        .playlist-title {
          font-size: 12px;
          text-transform: uppercase;
          letter-spacing: 0.14em;
          color: var(--muted);
          margin-bottom: 4px;
        }
    
        #playlist {
          list-style: none;
          padding: 0;
          margin: 0;
        }
    
        #playlist li {
          padding: 6px 8px;
          border-radius: 6px;
          font-size: 13px;
          cursor: pointer;
          display: flex;
          justify-content: space-between;
          align-items: center;
          color: #e5e5e5;
          transition: background 0.15s ease, color 0.15s ease;
        }
    
        #playlist li span.track-name {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
    
        #playlist li span.track-status {
          font-size: 10px;
          text-transform: uppercase;
          color: var(--accent);
          opacity: 0;
        }
    
        #playlist li:hover {
          background: #262626;
        }
    
        #playlist li.active {
          background: #262626;
          color: var(--accent);
        }
    
        #playlist li.active span.track-status {
          opacity: 1;
        }
    
        /* Scrollbar styling (optional) */
        .playlist::-webkit-scrollbar {
          width: 6px;
        }
        .playlist::-webkit-scrollbar-track {
          background: #151515;
          border-radius: 999px;
        }
        .playlist::-webkit-scrollbar-thumb {
          background: #333;
          border-radius: 999px;
        }
      </style>
    </head>
    <body>
      <div class="audio-player" aria-label="Custom audio player">
        <!-- Hidden native audio element -->
        <audio id="audio" preload="metadata"></audio>
    
        <!-- Track info -->
        <div class="track-info">
          <div class="cover" id="cover-art" aria-hidden="true">
            ♪
          </div>
          <div class="meta">
            <div class="title" id="track-title">Track title</div>
            <div class="artist" id="track-artist">Artist</div>
          </div>
        </div>
    
        <!-- Controls & progress -->
        <div class="controls-row">
          <div class="main-controls">
            <button id="prev" class="control-btn" aria-label="Previous track">⏮</button>
            <button id="play-pause" class="control-btn play" aria-label="Play">▶</button>
            <button id="next" class="control-btn" aria-label="Next track">⏭</button>
          </div>
    
          <div class="time-row">
            <span id="current-time">0:00</span>
            <input type="range" id="progress" class="slider" value="0" min="0" step="1">
            <span id="duration">0:00</span>
          </div>
    
          <div class="volume-control">
            <span class="volume-icon" id="volume-icon">🔊</span>
            <input type="range" id="volume" class="slider" min="0" max="1" step="0.01" value="0.8" aria-label="Volume">
          </div>
        </div>
    
        <!-- Playlist -->
        <div class="playlist">
          <div class="playlist-title">Playlist</div>
          <ul id="playlist"></ul>
        </div>
      </div>
    
      <script>
        // === Playlist data (replace file paths with your own) ===
        const tracks = [
          {
            title: "Song One",
            artist: "Artist A",
            src: "audio/song1.mp3",
            cover: "", // optional image URL, e.g. "covers/song1.jpg"
          },
          {
            title: "Song Two",
            artist: "Artist B",
            src: "audio/song2.mp3",
            cover: "",
          },
          {
            title: "Song Three",
            artist: "Artist C",
            src: "audio/song3.mp3",
            cover: "",
          }
        ];
    
        // === Element references ===
        const audio = document.getElementById("audio");
        const playPauseBtn = document.getElementById("play-pause");
        const prevBtn = document.getElementById("prev");
        const nextBtn = document.getElementById("next");
        const progress = document.getElementById("progress");
        const currentTimeEl = document.getElementById("current-time");
        const durationEl = document.getElementById("duration");
        const volumeSlider = document.getElementById("volume");
        const volumeIcon = document.getElementById("volume-icon");
        const trackTitleEl = document.getElementById("track-title");
        const trackArtistEl = document.getElementById("track-artist");
        const coverArtEl = document.getElementById("cover-art");
        const playlistEl = document.getElementById("playlist");
    
        let currentTrackIndex = 0;
        let isSeeking = false;
        let wasPlayingBeforeSeek = false;
    
        // === Utility functions ===
        function formatTime(seconds) {
          if (isNaN(seconds)) return "0:00";
          const m = Math.floor(seconds / 60);
          const s = Math.floor(seconds % 60);
          return `${m}:${s.toString().padStart(2, "0")}`;
        }
    
        function setPlayButtonState(isPlaying) {
          playPauseBtn.textContent = isPlaying ? "⏸" : "▶";
          playPauseBtn.setAttribute("aria-label", isPlaying ? "Pause" : "Play");
        }
    
        function updateVolumeIcon(volume) {
          if (volume === 0) {
            volumeIcon.textContent = "🔇";
          } else if (volume < 0.4) {
            volumeIcon.textContent = "🔈";
          } else if (volume < 0.8) {
            volumeIcon.textContent = "🔉";
          } else {
            volumeIcon.textContent = "🔊";
          }
        }
    
        // === Playlist UI ===
        function buildPlaylistUI() {
          playlistEl.innerHTML = "";
          tracks.forEach((track, index) => {
            const li = document.createElement("li");
            li.dataset.index = index;
    
            const nameSpan = document.createElement("span");
            nameSpan.className = "track-name";
            nameSpan.textContent = `${track.title} — ${track.artist}`;
    
            const statusSpan = document.createElement("span");
            statusSpan.className = "track-status";
            statusSpan.textContent = "● Playing";
    
            li.appendChild(nameSpan);
            li.appendChild(statusSpan);
    
            li.addEventListener("click", () => {
              if (index === currentTrackIndex) {
                // Toggle play/pause if clicking currently active track
                if (audio.paused) {
                  audio.play();
                } else {
                  audio.pause();
                }
              } else {
                loadTrack(index, true);
              }
            });
    
            playlistEl.appendChild(li);
          });
          highlightActiveTrack();
        }
    
        function highlightActiveTrack() {
          Array.from(playlistEl.children).forEach((li) => {
            li.classList.toggle("active", Number(li.dataset.index) === currentTrackIndex);
          });
        }
    
        // === Track loading & playback ===
        function loadTrack(index, autoplay = false) {
          if (index < 0) index = tracks.length - 1;
          if (index >= tracks.length) index = 0;
          currentTrackIndex = index;
    
          const track = tracks[index];
          audio.src = track.src;
          audio.load();
    
          trackTitleEl.textContent = track.title;
          trackArtistEl.textContent = track.artist;
          coverArtEl.textContent = track.title.charAt(0).toUpperCase() || "♪";
    
          if (track.cover) {
            coverArtEl.style.background = `center/cover no-repeat url("${track.cover}")`;
            coverArtEl.textContent = "";
          } else {
            coverArtEl.style.background = "linear-gradient(135deg, #353535, #121212)";
          }
    
          highlightActiveTrack();
    
          if (autoplay) {
            audio.play().catch(err => {
              // Autoplay may be blocked; keep button state in "paused"
              console.warn("Autoplay prevented by browser:", err);
              setPlayButtonState(false);
            });
          }
        }
    
        function playNext() {
          loadTrack(currentTrackIndex + 1, true);
        }
    
        function playPrev() {
          loadTrack(currentTrackIndex - 1, true);
        }
    
        // === Event listeners ===
        playPauseBtn.addEventListener("click", () => {
          if (audio.paused) {
            audio.play();
          } else {
            audio.pause();
          }
        });
    
        prevBtn.addEventListener("click", playPrev);
        nextBtn.addEventListener("click", playNext);
    
        audio.addEventListener("play", () => {
          setPlayButtonState(true);
          highlightActiveTrack();
        });
    
        audio.addEventListener("pause", () => {
          setPlayButtonState(false);
        });
    
        audio.addEventListener("loadedmetadata", () => {
          progress.max = Math.floor(audio.duration) || 0;
          durationEl.textContent = formatTime(audio.duration);
        });
    
        audio.addEventListener("timeupdate", () => {
          if (!isSeeking) {
            progress.value = Math.floor(audio.currentTime) || 0;
          }
          currentTimeEl.textContent = formatTime(audio.currentTime);
        });
    
        audio.addEventListener("ended", () => {
          playNext();
        });
    
        // Seek bar (progress)
        progress.addEventListener("input", () => {
          if (!isSeeking) {
            wasPlayingBeforeSeek = !audio.paused;
            isSeeking = true;
          }
          const seekTime = Number(progress.value);
          currentTimeEl.textContent = formatTime(seekTime);
        });
    
        progress.addEventListener("change", () => {
          const seekTime = Number(progress.value);
          audio.currentTime = seekTime;
          if (wasPlayingBeforeSeek) {
            audio.play();
          }
          isSeeking = false;
        });
    
        // Volume
        volumeSlider.addEventListener("input", () => {
          audio.volume = Number(volumeSlider.value);
          updateVolumeIcon(audio.volume);
        });
    
        // Keyboard shortcuts (optional)
        window.addEventListener("keydown", (e) => {
          if (e.target.tagName === "INPUT") return; // don't interfere with sliders
          switch (e.key) {
            case " ":
              e.preventDefault();
              if (audio.paused) audio.play(); else audio.pause();
              break;
            case "ArrowRight":
              audio.currentTime = Math.min(audio.currentTime + 5, audio.duration || audio.currentTime);
              break;
            case "ArrowLeft":
              audio.currentTime = Math.max(audio.currentTime - 5, 0);
              break;
          }
        });
    
        // === Init ===
        audio.volume = Number(volumeSlider.value);
        updateVolumeIcon(audio.volume);
        buildPlaylistUI();
        loadTrack(0, false);
      </script>
    </body>
    </html>
    

    Usage notes:

    • Put your audio files in an audio/ folder (or any path you like) and adjust the src values in the tracks array.
    • Optionally set cover to an image URL for each track.
    • Open this HTML file in a browser to see and interact with the custom player.

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