Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <title>My Example</title> <!-- Place the following CSS in your <head> or stylesheet --> <style> /* This is an optional body style to provide a background for the page */ body { background-color: #f3f4f-6; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif; } /* Optional: a container to center the card on the page for demonstration */ .demo-container-audio { display: flex; justify-content: center; padding: 2rem; } /* === Audio Player Card Styles === */ .audio-player-card { --player-radius: 12px; --player-shadow: 0 4px 10px rgba(0,0,0,0.1); --player-primary-color: #4f46e5; /* Indigo */ background-color: #ffffff; border-radius: var(--player-radius); box-shadow: var(--player-shadow); max-width: 400px; width: 100%; padding: 1.25rem; display: flex; align-items: center; gap: 1rem; } /* Play/Pause Button */ .ap-play-button { flex-shrink: 0; width: 48px; height: 48px; background-color: var(--player-primary-color); border-radius: 50%; border: none; cursor: pointer; color: #ffffff; display: flex; align-items: center; justify-content: center; transition: background-color 0.2s ease, transform 0.2s ease; } .ap-play-button:hover { background-color: #4338ca; transform: scale(1.05); } .ap-play-button svg { width: 24px; height: 24px; } /* Display 'pause' icon when the player is playing */ .audio-player-card[data-playing="true"] .ap-icon-play { display: none; } .ap-icon-pause { display: none; } .audio-player-card[data-playing="true"] .ap-icon-pause { display: block; } /* Main controls area */ .ap-controls { flex-grow: 1; display: flex; flex-direction: column; } .ap-info { font-size: 1rem; font-weight: 500; color: #1f2937; margin-bottom: 0.5rem; } /* Progress bar and time stamps */ .ap-progress-container { height: 6px; background-color: #e5e7eb; border-radius: 6px; cursor: pointer; } .ap-progress-bar { height: 100%; width: 0%; background-color: var(--player-primary-color); border-radius: 6px; } .ap-time-info { display: flex; justify-content: space-between; font-size: 0.75rem; color: #6b7280; margin-top: 0.25rem; } </style> <!-- Place the following HTML in your <body> --> <div class="demo-container-audio"> <div class="audio-player-card" data-playing="false"> <button class="ap-play-button" type="button" aria-label="Play/Pause"> <svg class="ap-icon-play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M4.5 5.653c0-1.426 1.529-2.33 2.779-1.643l11.54 6.647c1.295.748 1.295 2.535 0 3.284L7.279 20.99c-1.25.715-2.779-.217-2.779-1.643V5.653z" clip-rule="evenodd"/></svg> <svg class="ap-icon-pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"> <rect x="5.5" y="5" width="3" height="14" rx="0.5" /> <rect x="15.5" y="5" width="3" height="14" rx="0.5" /> </svg> </button> <div class="ap-controls"> <div class="ap-info">Podcast Intro Music</div> <div class="ap-progress-container"> <div class="ap-progress-bar"></div> </div> <div class="ap-time-info"> <span class="ap-current-time">0:00</span> <span class="ap-duration">0:00</span> </div> </div> <!-- This is the actual audio element, hidden from view --> <audio class="ap-audio-element" preload="metadata"> <source src="/music/good_enough.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> </div> </div> <!-- Place the following JavaScript before your closing </body> tag --> <script> document.addEventListener('DOMContentLoaded', () => { const player = document.querySelector('.audio-player-card'); if (!player) return; const audio = player.querySelector('.ap-audio-element'); const playBtn = player.querySelector('.ap-play-button'); const progressBar = player.querySelector('.ap-progress-bar'); const progressContainer = player.querySelector('.ap-progress-container'); const currentTimeEl = player.querySelector('.ap-current-time'); const durationEl = player.querySelector('.ap-duration'); // Function to format time in MM:SS function formatTime(seconds) { const minutes = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${minutes}:${secs < 10 ? '0' : ''}${secs}`; } // Update progress bar function updateProgress() { const percent = (audio.currentTime / audio.duration) * 100; progressBar.style.width = `${percent}%`; currentTimeEl.textContent = formatTime(audio.currentTime); } // Set duration once audio metadata loads audio.addEventListener('loadedmetadata', () => { durationEl.textContent = formatTime(audio.duration); }); // Toggle play/pause playBtn.addEventListener('click', () => { const isPlaying = player.dataset.playing === 'true'; if (isPlaying) { audio.pause(); } else { audio.play(); } }); audio.addEventListener('play', () => { player.dataset.playing = 'true'; }); audio.addEventListener('pause', () => { player.dataset.playing = 'false'; }); // Update time and progress bar audio.addEventListener('timeupdate', updateProgress); // Seek on progress bar click progressContainer.addEventListener('click', (e) => { const width = progressContainer.clientWidth; const clickX = e.offsetX; const duration = audio.duration; audio.currentTime = (clickX / width) * duration; }); }); </script>