LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Full Featured Custom Video Player Using HTML5 And JavaScript

Full Featured Custom Video Player Using HTML5 And JavaScript

Full--Featured-Custom-Video-Player-Using-HTML5-And-JavaScript
Video Player is must have a tool for a website that has videos to play what about if you do not want to use the browser default player for your videos that do not match your website styling and demand then you have to build your own custom video player.

So, in this tutorial, we will show you how to create your own custom video player using HTML5, JavaScript, and CSS. You may also like a custom audio player using HTML5.

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.

That’s all, this is how to create your own custom video player using HTML5, JavaScript, and CSS. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Cross Section Hover On Image With Read More Link

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add JavaScript Audio Waveform Visualizer Player With Pure Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style type="text/css">
*,
*::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%;
}
}
</style>

HTML:

<div class="video-container">
<video src="https://custom-html5-video.surge.sh/video.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> 

JavaScript:

<script type="text/javascript">
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);
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
5 Stylish And Awesome Pure CSS Tooltip Widgets

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *