Web Audio Player - NesiaNet

09 April 2024
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Modern Audio Player</title>
  <style>
    body {
      font-family: 'Segoe UI', sans-serif;
      background: linear-gradient(135deg, #1f1c2c, #928dab);
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      margin: 0;
    }
    .player {
      background-color: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(10px);
      padding: 2rem;
      border-radius: 20px;
      box-shadow: 0 0 20px rgba(0,0,0,0.5);
      width: 320px;
    }
    .controls {
      display: flex;
      justify-content: space-between;
      margin: 1rem 0;
    }
    .controls button {
      background: none;
      border: none;
      color: white;
      font-size: 1.5rem;
      cursor: pointer;
    }
    .volume {
      width: 100%;
      margin: 1rem 0;
    }
    .seekbar {
      width: 100%;
    }
    .playlist {
      margin-top: 1rem;
      max-height: 150px;
      overflow-y: auto;
    }
    .playlist li {
      cursor: pointer;
      padding: 0.5rem;
      border-bottom: 1px solid rgba(255,255,255,0.2);
    }
    .playlist li.active {
      background-color: rgba(255,255,255,0.2);
    }
  </style>
</head>
<body>
  <div class="player">
    <input type="file" id="fileInput" multiple accept="audio/*">
    <audio id="audio" controls hidden></audio>
    <div class="controls">
      <button id="prev">⏮️</button>
      <button id="play">▶️</button>
      <button id="next">⏭️</button>
    </div>
    <input type="range" id="seekbar" class="seekbar" min="0" max="100" value="0">
    <input type="range" id="volume" class="volume" min="0" max="1" step="0.01" value="1">
    <ul id="playlist" class="playlist"></ul>
  </div>

  <script>
    const fileInput = document.getElementById('fileInput');
    const audio = document.getElementById('audio');
    const playBtn = document.getElementById('play');
    const prevBtn = document.getElementById('prev');
    const nextBtn = document.getElementById('next');
    const seekbar = document.getElementById('seekbar');
    const volume = document.getElementById('volume');
    const playlist = document.getElementById('playlist');

    let tracks = [];
    let currentTrackIndex = 0;
    let isPlaying = false;
    let isSeeking = false;

    fileInput.addEventListener('change', (e) => {
      const newFiles = Array.from(e.target.files);
      newFiles.forEach((track) => {
        tracks.push(track);
        const li = document.createElement('li');
        li.textContent = track.name;
        li.addEventListener('click', () => {
          currentTrackIndex = tracks.indexOf(track);
          loadTrack();
          playAudio();
        });
        playlist.appendChild(li);
      });
      if (tracks.length === newFiles.length) {
        loadTrack();
      }
    });

    function loadTrack() {
      if (!tracks.length) return;
      const track = tracks[currentTrackIndex];
      const url = URL.createObjectURL(track);
      audio.src = url;
      updatePlaylistUI();
    }

    function playAudio() {
      audio.play();
      isPlaying = true;
      playBtn.textContent = '⏸️';
    }

    function pauseAudio() {
      audio.pause();
      isPlaying = false;
      playBtn.textContent = '▶️';
    }

    playBtn.addEventListener('click', () => {
      isPlaying ? pauseAudio() : playAudio();
    });

    prevBtn.addEventListener('click', () => {
      currentTrackIndex = (currentTrackIndex - 1 + tracks.length) % tracks.length;
      loadTrack();
      playAudio();
    });

    nextBtn.addEventListener('click', () => {
      currentTrackIndex = (currentTrackIndex + 1) % tracks.length;
      loadTrack();
      playAudio();
    });

    audio.addEventListener('timeupdate', () => {
      if (!isSeeking && audio.duration) {
        seekbar.value = (audio.currentTime / audio.duration) * 100;
      }
    });

    seekbar.addEventListener('input', () => {
      isSeeking = true;
    });

    seekbar.addEventListener('change', () => {
      if (audio.duration) {
        audio.currentTime = (seekbar.value / 100) * audio.duration;
      }
      isSeeking = false;
    });

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

    audio.addEventListener('ended', () => {
      nextBtn.click();
    });

    function updatePlaylistUI() {
      const items = playlist.querySelectorAll('li');
      items.forEach((item, index) => {
        item.classList.toggle('active', index === currentTrackIndex);
      });
    }
  </script>
</body>
</html>