This JavaScript code snippet helps you to create an HTML5 video player with custom controls. It renders a custom play/pause button, progress bar, volume, and fullscreen button inside the interface of the video player.
How to Create Custom HTML5 Video Player with JavaScript
1. First of all, create the HTML structure as follows:
<div class="video-container"> <video src="https://testerapidoteste.000webhostapp.com/DBZ.mp4" class="video" width="700"></video> <div class="video-controls video-controls-visibility--visible"> <button class="control-item play-and-pause-video fa fa-play" title="Play or Pause"></button> <div class="progress-video-container"> <input type="range" class="control-item progress-video" min="0.0" value="0.00" step="any" max="100.00"> <span class="progress-time time-video">00:00</span> <span class="duration-time time-video">00:00</span> </div> <div class="audio-video-container"> <button class="control-item volume-video fa fa-volume-up" title="Volume"></button> <input type="range" class="control-item slide-volume-video" min="0" value="1" step="any" max="1"> </div> <button class="control-item fullscreen-video fa fa-expand"></button> </div> </div>
2. After that, add the following CSS styles to your project:
*, *::before, *::after { margin: 0; padding: 0; border: none; box-sizing: border-box; } :root { --colorBtnPrincipal: #D2D2D2; --rangeBg: #333; } video { max-width: 100%; } button { background: none; color: var(--colorBtnPrincipal); outline: none; } button, input { cursor: pointer; } /* VIDEO CONTAINER */ .video-container { position: relative; max-width: 700px; } .video { max-width: 100%; border-radius: 5px; } .video::-webkit-media-controls, .video::-webkit-media-controls-enclosure { display:none !important; } /* VIDEO CONTROLS */ .video:-moz-full-screen + .video-controls { bottom: 0; } .video:-webkit-full-screen + .video-controls { bottom: 0; } .video-controls { display: flex; justify-content: space-around; align-items: center; position: absolute; bottom: 4px; left: 0; z-index: 2147483647; padding: 10px; width: 100%; border-radius: 0 0 5px 5px; background-color: rgba(0,0,0,0.3); } .play-and-pause-video { padding: 8px 12px; width: 40px; box-shadow: 2px 2px 3px rgba(0,0,0,0.5); border-radius: 4px; background-color: crimson; } /* VIDEO PROGRESS */ .progress-video-container { display: flex; align-items: center; position: relative; width: 65%; } .time-video { font-family: sans-serif; font-size: 16px; font-weight: bold; color: var(--colorBtnPrincipal); } .progress-time { order: -1;} .progress-video, .slide-volume-video { background-image: linear-gradient(crimson, crimson); background-repeat: no-repeat; background-size: 0% 100%; } .progress-video, .slide-volume-video { width: 100%; height: 10px; margin: 0 10px; -webkit-appearance: none; border-radius: 4px; outline: none; background-color: var(--rangeBg); } /* ESTILOS COMUNS */ .progress-video::-moz-range-thumb, .slide-volume-video::-moz-range-thumb { width: 15px; height: 15px; background-color: var(--colorBtnPrincipal); border: none; } .slide-volume-video::-webkit-slider-thumb, .progress-video::-webkit-slider-thumb { width: 15px; height: 15px; -webkit-appearance: none; border-radius: 50%; background-color: var(--colorBtnPrincipal); } .progress-video::-moz-range-track, .slide-volume-video::-moz-range-track { height: 0; background-color: var(--rangeBg); border-radius: 4px; } .control-item { font-size: 18px; } /* VIDEO AUDIO */ .slide-volume-video { width: 60px; display: none; transition: display .2s ease-out; margin-left: -2px; } .audio-video-container:hover > .slide-volume-video{ display: inline-block; } /* MEDIAS QUERIES* */ @media (max-width: 320px) { .control-item { font-size: 10px; } .progress-video-container { width: 60%; } .slide-volume-video { width: 40px; } .time-video {font-size: 10px; } } @media (max-width: 480px){ .video-controls { justify-content: space-around; padding: 10px 0px; } .control-item { font-size: 16px; } .time-video { font-size: 12px; } .play-and-pause-video { width: 15px; padding: 0; background: none; box-shadow: none; } .progress-video { margin: 0 3px;} .slide-volume-video { width: 45px; } } @media (min-width: 1200){ .video:-webkit-full-screen + .video-controls > .progress-video-container { width: 100%; } }
3. Finally, add the following JavaScript code and done.
const $VIDEO = document.querySelector('.video'), $VIDEO_CONTROLS = document.querySelector('.video-controls'), $BUTTON_PAUSE_AND_PLAY = document.querySelector('.play-and-pause-video'), $PROGRESS_VIDEO = document.querySelector('.progress-video'), $CHANGE_VOLUME = document.querySelector('.slide-volume-video'), $FULLSCREEN = document.querySelector('.fullscreen-video'); function durationVideo() { let durationMidia = $VIDEO.duration, $durationTime = document.querySelector('.duration-time'); $durationTime.innerHTML = transformVideoDuration(durationMidia); animationVolume($VIDEO.volume); }; function progressVideo() { var autoProgress = $VIDEO.currentTime, $progressBar = document.querySelector('.progress-video'), $progressTime = document.querySelector('.progress-time'); $progressBar.value = autoProgress.toFixed(0); $progressBar.setAttribute('max', $VIDEO.duration); $progressTime.innerHTML = transformVideoDuration(autoProgress); animationProgress(); }; function ChangeProgressVideo() { $changeProgress = document.querySelector('.progress-video'); $VIDEO.currentTime = $changeProgress.value; }; function animationProgress() { let percentageProgress = (($PROGRESS_VIDEO.value - $PROGRESS_VIDEO.min) * 100) / ($PROGRESS_VIDEO.max - $PROGRESS_VIDEO.min); $PROGRESS_VIDEO.style.backgroundSize = `${percentageProgress}% 100%`; console.log('progress: ' + percentageProgress) }; function animationVolume(volume) { let animationVolume = volume; animationVolume = volume * 100; if (animationVolume === 100) { animationVolume = 100; } $CHANGE_VOLUME.style.backgroundSize = `${animationVolume}% 100%`; }; function transformVideoDuration(timeVideo) { let hours, mins, secds, time; hours = Math.floor(timeVideo / 3600); mins = Math.floor(timeVideo / 60); secds = Math.floor(timeVideo - mins * 60) return time = formartTimeVideo(hours, mins, secds); }; function formartTimeVideo(hours, mins, secds) { let time; if (hours < 1) { hours = ''; }; if (hours < 10 && hours != '') { hours = '0' + hours + ':'; }; if (mins < 10) { mins = '0' + mins; } if (secds < 10) { secds = '0' + secds; } return time = `${hours}${mins}:${secds}`; }; function playAndPause() { let $playButton = document.querySelector('.play-and-pause-video'); if ($VIDEO.paused == true) { playVideo(); $playButton.classList.remove('fa-play'); $playButton.classList.add('fa-pause'); } else { pauseVideo(); $playButton.classList.remove('fa-pause'); $playButton.classList.add('fa-play'); } }; function pauseVideo(){ $VIDEO.pause(); }; function playVideo(){ $VIDEO.play(); }; function volume() { let $changeVolume = document.querySelector('.slide-volume-video').value; $VIDEO.volume = $changeVolume; let $buttonVolume = document.querySelector('.volume-video'); if ($VIDEO.volume === 0) { $buttonVolume.classList.remove('fa-volume-up'); $buttonVolume.classList.add('fa-volume-off'); } else { $buttonVolume.classList.remove('fa-volume-off'); $buttonVolume.classList.add('fa-volume-up'); }; animationVolume($changeVolume); }; function endVideo(){ let $playButtonEnd = document.querySelector('.play-and-pause-video'); $VIDEO_CONTROLS.classList.remove('video-controls-visibility--hidden'); $playButtonEnd.classList.remove('fa-pause'); $playButtonEnd.classList.add('fa-play'); } function videoFullScreen() { if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { if ($VIDEO.requestFullscreen) { $VIDEO.requestFullscreen(); } else if ($VIDEO.msRequestFullscreen) { $VIDEO.msRequestFullscreen(); } else if ($VIDEO.mozRequestFullScreen) { $VIDEO.mozRequestFullScreen(); } else if ($VIDEO.webkitRequestFullscreen) { $VIDEO.webkitRequestFullscreen(); } $FULLSCREEN.classList.remove('fa-expand'); $FULLSCREEN.classList.add('fa-compress'); } else { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } $FULLSCREEN.classList.remove('fa-compress'); $FULLSCREEN.classList.add('fa-expand'); }; }; function controlVisibility(){ setTimeout(function(){ $VIDEO_CONTROLS.classList.remove('video-controls-visibility--visible'); $VIDEO_CONTROLS.classList.add('video-controls-visibility--hidden'); }, 10000) console.log('play') }; // EVENTS PLAYER // EVENTS VIDEO $VIDEO.addEventListener('loadeddata', durationVideo); $VIDEO.addEventListener('timeupdate', progressVideo); $VIDEO.addEventListener('play', controlVisibility); $VIDEO.addEventListener('click', playAndPause); $VIDEO.addEventListener('ended', endVideo); // EVENTS VIDEO CONTROLS $PROGRESS_VIDEO.addEventListener('change', ChangeProgressVideo) $BUTTON_PAUSE_AND_PLAY.addEventListener('click', playAndPause); $CHANGE_VOLUME.addEventListener('change', volume); $FULLSCREEN.addEventListener('click', videoFullScreen);
That’s all! hopefully, you have successfully integrated this video player code snippet into your project. If you have any questions or facing any issues, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.