/* LIB */
function MyPlayer(playerContainer) {
let videoElement;
let videoPoster;
const instance = {};
instance.stop = () => {
return new Promise(resolve => {
videoElement.pause();
videoElement.src = null;
setTimeout(() => {
resolve();
});
});
};
instance.setUrl = async(url) => {
await instance.stop();
videoElement.src = url;
videoElement.load();
};
instance.play = () => {
videoElement.play();
};
instance.playUrl = async(url) => {
await instance.setUrl(url);
instance.play();
};
instance.pause = () => {
videoElement.pause();
};
instance.setPoster = (url) => {
if(url) {
videoPoster.style.backgroundImage = 'url(' + url + ')';
} else {
videoPoster.style.backgroundImage = null;
}
};
const onPlayerEvent = (eventName, event) => {
playerContainer.setAttribute('data-state', eventName);
};
const onCoverClick = (event) => {
event.preventDefault();
event.stopPropagation();
if(videoElement.paused) {
instance.play();
} else {
instance.pause();
}
};
/* Player init */
playerContainer.className += ' MyPlayer';
playerContainer.setAttribute('data-state', 'emptied');
playerContainer.innerHTML = '<video class="videoElement" poster="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"></video><div class="videoPoster"></div><div class="videoIndicator"></div><div class="playerLogo">MyPlayer</div><div class="videoCover"></div>';
videoElement = playerContainer.getElementsByClassName('videoElement')[0];
videoElement.addEventListener('emptied', onPlayerEvent.bind(this, 'emptied'), false);
videoElement.addEventListener('loadstart', onPlayerEvent.bind(this, 'loadstart'), false);
videoElement.addEventListener('canplay', onPlayerEvent.bind(this, 'canplay'), false);
videoElement.addEventListener('playing', onPlayerEvent.bind(this, 'playing'), false);
videoElement.addEventListener('pause', onPlayerEvent.bind(this, 'pause'), false);
videoElement.addEventListener('waiting', onPlayerEvent.bind(this, 'waiting'), false);
videoElement.addEventListener('ended', onPlayerEvent.bind(this, 'ended'), false);
videoElement.addEventListener('error', onPlayerEvent.bind(this, 'error'), false);
videoPoster = playerContainer.getElementsByClassName('videoPoster')[0];
const videoCover = playerContainer.getElementsByClassName('videoCover')[0];
videoCover.addEventListener('click', onCoverClick, false);
videoCover.addEventListener('touch', onCoverClick, false);
return instance;
}
/* USAGE */
const playerInstance = MyPlayer(document.getElementById('myPlayer'));
playerInstance.setPoster('https://www.w3schools.com/html/pic_trulli.jpg');
playerInstance.playUrl('https://www.w3schools.com/html/mov_bbb.mp4');
const playerInstance2 = MyPlayer(document.getElementById('myPlayer2'));
playerInstance2.setPoster('https://www.w3schools.com/html/pic_trulli.jpg');
playerInstance2.setUrl('https://www.w3schools.com/html/mov_bbb.mp4');
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
.MyPlayer {
position: relative;
}
.MyPlayer .videoElement {
background-color: black;
width: 100%;
height: 100%;
vertical-align: bottom;
}
.MyPlayer .videoPoster, .MyPlayer .videoCover {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.MyPlayer .videoPoster {
opacity: 0;
background-size: cover;
transition: opacity .2s;
}
.MyPlayer[data-state="emptied"] .videoPoster, .MyPlayer[data-state="loadstart"] .videoPoster, .MyPlayer[data-state="canplay"] .videoPoster, .MyPlayer[data-state="ended"] .videoPoster {
opacity: 1;
}
.MyPlayer .videoIndicator::before {
position: absolute;
top: 10px;
left: 10px;
color: #fff;
text-shadow: 0 0 5px #000;
}
.MyPlayer[data-state="emptied"] .videoIndicator::before {
content: '';
}
.MyPlayer[data-state="loadstart"] .videoIndicator::before, .MyPlayer[data-state="waiting"] .videoIndicator::before {
content: '\25CC';
font-size: 1.6em;
animation:spin 4s linear infinite;
}
.MyPlayer[data-state="canplay"] .videoIndicator::before {
content: '\25B6';
font-size: 3em;
right: 10px;
text-align: center;
}
.MyPlayer[data-state="playing"] .videoIndicator::before {
content: '\25B6';
font-size: 1.3em;
}
.MyPlayer[data-state="pause"] .videoIndicator::before {
content: '\2590\A0\258C';
}
.MyPlayer[data-state="ended"] .videoIndicator::before {
content: '\27F2';
font-size: 1.3em;
}
.MyPlayer[data-state="error"] .videoIndicator::before {
content: '\2716';
}
.MyPlayer .playerLogo {
position: absolute;
right: 10px;
bottom: 10px;
font-size: 1.4em;
color: #fff;
text-shadow: 0 0 5px #f00;
transition: color .2s, text-shadow .2s;
}
.MyPlayer[data-state="playing"]:not(:hover) .playerLogo {
color: rgba(255, 255, 255, 0.4);
text-shadow: 0 0 5px rgba(255, 0, 0, 0.4);
}
<div id="myPlayer" style="display:inline-block;max-width:250px;"></div> <div id="myPlayer2" style="display:inline-block;max-width:225px;"></div><br />
Video files from https//www.w3schools.com<br />
Video from https://www.bigbuckbunny.org/