Custom Html5 Video Player Codepen -

// Video initial state let isPlaying = false; let wasPlayingBeforeSeek = false; // Helper: format time (seconds) -> MM:SS function formatTime(seconds) if (isNaN(seconds)) return "00:00"; const hrs = Math.floor(seconds / 3600); const mins = Math.floor((seconds % 3600) / 60); const secs = Math.floor(seconds % 60); if (hrs > 0) return `$hrs.toString().padStart(2, '0'):$mins.toString().padStart(2, '0'):$secs.toString().padStart(2, '0')`; return `$mins.toString().padStart(2, '0'):$secs.toString().padStart(2, '0')`; // Update time display and progress bar fill function updateProgressAndTime() // Play/Pause toggle UI function updatePlayPauseUI() if (video.paused) playIcon.classList.remove('fa-pause'); playIcon.classList.add('fa-play'); isPlaying = false; else playIcon.classList.remove('fa-play'); playIcon.classList.add('fa-pause'); isPlaying = true; function togglePlayPause() if (video.paused) video.play().catch(e => console.warn("Playback failed:", e)); else video.pause(); updatePlayPauseUI(); // Event listeners for video native events video.addEventListener('play', () => updatePlayPauseUI(); ); video.addEventListener('pause', () => updatePlayPauseUI(); ); video.addEventListener('timeupdate', updateProgressAndTime); video.addEventListener('loadedmetadata', () => ); video.addEventListener('volumechange', () => video.volume === 0); ); // Progress bar seeking (click & drag) let seeking = false; function seekFromEvent(e) const rect = progressBar.getBoundingClientRect(); let clickX = e.clientX - rect.left; clickX = Math.min(Math.max(clickX, 0), rect.width); const percent = clickX / rect.width; if (video.duration && isFinite(video.duration)) const newTime = percent * video.duration; video.currentTime = newTime; updateProgressAndTime(); progressBar.addEventListener('mousedown', (e) => seeking = true; // store play state before seek wasPlayingBeforeSeek = !video.paused; if (!video.paused) video.pause(); seekFromEvent(e); e.preventDefault(); ); window.addEventListener('mousemove', (e) => if (seeking) seekFromEvent(e); ); window.addEventListener('mouseup', () => if (seeking) if (wasPlayingBeforeSeek) video.play().catch(err => console.log("auto resume error", err)); seeking = false; ); // optional: touch support for progress bar progressBar.addEventListener('touchstart', (e) => e.preventDefault(); seeking = true; wasPlayingBeforeSeek = !video.paused; if (!video.paused) video.pause(); const touch = e.touches[0]; const fakeEvent = clientX: touch.clientX ; seekFromEvent(fakeEvent); ); window.addEventListener('touchmove', (e) => if (seeking && e.touches.length) const touch = e.touches[0]; const fakeEvent = clientX: touch.clientX ; seekFromEvent(fakeEvent); ); window.addEventListener('touchend', () => { if (seeking) { if (wasPlayingBeforeSeek) { video.play().catch(()=>{}); } seeking = false; } }); // Volume & mute function updateMuteIcon(isMuted) const icon = muteBtn.querySelector('i'); if (isMuted volumeSlider.addEventListener('input', (e) => const val = parseFloat(e.target.value); video.volume = val; video.muted = false; updateMuteIcon(false); ); muteBtn.addEventListener('click', () => if (video.muted) video.muted = false; // restore previous volume if needed, but keep slider value if (video.volume === 0) video.volume = 0.5; volumeSlider.value = video.volume; else video.muted = true; updateMuteIcon(video.muted); ); // Playback Speed speedSelect.addEventListener('change', (e) => video.playbackRate = parseFloat(e.target.value); ); // Fullscreen functionality function toggleFullscreen() const container = document.querySelector('.player-container'); if (!document.fullscreenElement) container.requestFullscreen().catch(err => console.warn(`Fullscreen error: $err.message`); ); else document.exitFullscreen(); fullscreenBtn.addEventListener('click', toggleFullscreen); // Also optional: double click on video to fullscreen videoWrapper.addEventListener('dblclick', (e) => e.stopPropagation(); toggleFullscreen(); ); // Click on video to play/pause video.addEventListener('click', (e) => e.stopPropagation(); togglePlayPause(); ); // Play/Pause button click playPauseBtn.addEventListener('click', (e) => e.stopPropagation(); togglePlayPause(); ); // Keyboard controls: space, k, arrow left/right, up/down, f fullscreen window.addEventListener('keydown', (e) => const tag = e.target.tagName; if (tag === 'INPUT' ); // Initial volume set video.volume = 0.8; volumeSlider.value = 0.8; video.muted = false; // If video fails to load any metadata, ensure default video.addEventListener('error', () => console.warn("Video source error, but sample should work. Check internet."); timeDisplay.innerText = "00:00 / 00:00"; ); // small style for buffering: not needed, but elegant updateProgressAndTime(); })(); </script> </body> </html>

.ctrl-btn:active transform: scale(0.96);

/* CUSTOM CONTROLS BAR */ .custom-controls background: rgba(10, 14, 23, 0.92); backdrop-filter: blur(12px); padding: 0.9rem 1.2rem; display: flex; flex-wrap: wrap; align-items: center; gap: 0.8rem; border-top: 1px solid rgba(255, 255, 255, 0.12); transition: all 0.2s;

<!-- Volume control with icon --> <div class="volume-wrapper"> <button class="ctrl-btn" id="muteBtn" style="background:transparent; width:32px; height:32px;" aria-label="Mute"> <i class="fas fa-volume-up"></i> </button> <input type="range" id="volumeSlider" class="volume-slider" min="0" max="1" step="0.02" value="0.8"> </div> custom html5 video player codepen

.progress-bar:hover height: 8px;

/* loading / overlay (optional subtle hint) */ .video-wrapper::after content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; background: radial-gradient(circle at center, transparent 60%, rgba(0,0,0,0.1)); opacity: 0; transition: opacity 0.2s; /* small badge (just for style) */ .player-footer background: rgba(0,0,0,0.3); text-align: center; font-size: 0.7rem; padding: 0.5rem; color: #94a3b8; letter-spacing: 0.3px; border-top: 1px solid rgba(255,255,255,0.05); a color: #7aa2f7; text-decoration: none; </style> <!-- FontAwesome Icons (free CDN) for nice icons --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> </head> <body>

/* BUTTONS STYLE */ .ctrl-btn background: transparent; border: none; color: #f0f3fa; font-size: 1.35rem; width: 40px; height: 40px; border-radius: 40px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s cubic-bezier(0.2, 0.9, 0.4, 1.1); background: rgba(255, 255, 255, 0.05); // Video initial state let isPlaying = false;

/* PROGRESS BAR TRACK */ .progress-bar flex: 1; height: 5px; background: rgba(255, 255, 255, 0.25); border-radius: 12px; cursor: pointer; position: relative; transition: height 0.1s;

<!-- Progress Bar --> <div class="progress-container"> <div class="progress-bar" id="progressBar"> <div class="progress-filled" id="progressFilled"></div> </div> </div>

/* PLAY/PAUSE BIG ICON */ .play-pause-btn background: rgba(59, 130, 246, 0.85); color: white; font-weight: bold; .play-pause-btn:hover background: #3b82f6; let wasPlayingBeforeSeek = false

video width: 100%; height: auto; display: block; vertical-align: middle;

.ctrl-btn:hover background: rgba(255, 255, 255, 0.2); transform: scale(1.02); color: white;

<!-- Fullscreen button --> <button class="ctrl-btn fullscreen-btn" id="fullscreenBtn" aria-label="Fullscreen"> <i class="fas fa-expand"></i> </button> </div> <div class="player-footer"> 🎬 Custom HTML5 Video Player • Click video to play/pause • Drag progress & volume </div> </div>

<!-- Time display --> <div class="time-display" id="timeDisplay"> 00:00 / 00:00 </div>

.volume-slider::-webkit-slider-thumb -webkit-appearance: none; width: 12px; height: 12px; background: #ffffff; border-radius: 50%; cursor: pointer; box-shadow: 0 0 4px #3b82f6;