(function() { window.VideoPlayer = function(options) { var self = {}, that; self.options = { autoplay: false, controls: true, items: [], loop: false, muted: false, playbackRate: 1, position: 0, volume: 1 } Object.assign(self.options, options); that = VideoElement(options); self.controls = document.createElement('div') self.controls.classList.add('mx-controls') if (options.poster) { self.controls.classList.add('poster') } //self.controls.style.display = "none" if (self.options.controls) { var ratio = `aspect-ratio: ${self.options.aspectratio};` if (!self.options.aspectratio) { ratio = 'height: 128px;' } self.controls.innerHTML = `
${icon.play}
${icon.mute}
${isIOS || !self.options.aspectratio ? "" : icon.enterFullscreen}
` var toggleVideo = event => { event.preventDefault() event.stopPropagation() if (that.paused) { self.controls.classList.remove('poster') that.play() } else { that.pause() } } async function toggleFullscreen(event) { if (isIOS) { return } event.preventDefault() event.stopPropagation() if (!document.fullscreenElement) { that.classList.add('fullscreen') if (that.webkitRequestFullscreen) { await that.webkitRequestFullscreen() } else { await that.requestFullscreen() } console.log('entered fullscreen') var failed = false if (!screen.orientation.type.startsWith("landscape")) { await screen.orientation.lock("landscape").catch(err => { console.log('no luck with lock', err) /* document.querySelector('.error').innerHTML = '' + err that.classList.remove('fullscreen') document.exitFullscreen(); screen.orientation.unlock() failed = true */ }) } if (that.paused && !failed) { self.controls.classList.remove('poster') that.play() } } else { that.classList.remove('fullscreen') document.exitFullscreen(); screen.orientation.unlock() } } var toggleSound = event => { event.preventDefault() event.stopPropagation() if (that.muted()) { that.muted(false) } else { that.muted(true) } } var showControls function hideControlsLater() { if (showControls) { clearTimeout(showControls) } showControls = setTimeout(() => { if (touching) { hideControlsLater() } else { self.controls.style.opacity = that.paused ? '1' : '0' showControls = null } }, 3000) } var toggleControls = event => { if (event.target.tagName == "INPUT") { if (showControls) { clearTimeout(showControls) } return } if (self.controls.style.opacity == '0') { event.preventDefault() event.stopPropagation() self.controls.style.opacity = '1' hideControlsLater() } else { self.controls.style.opacity = '0' } } self.controls.addEventListener("mousemove", event => { if (showControls) { clearTimeout(showControls) } self.controls.style.opacity = '1' hideControlsLater() }) self.controls.addEventListener("mouseleave", event => { if (showControls) { clearTimeout(showControls) } self.controls.style.opacity = that.paused ? '1' : '0' showControls = null }) self.controls.addEventListener("touchstart", event => { touching = true }) self.controls.addEventListener("touchstart", toggleControls) self.controls.addEventListener("touchend", event => { touching = false }) self.controls.querySelector('.toggle').addEventListener("click", toggleVideo) self.controls.querySelector('.volume').addEventListener("click", toggleSound) self.controls.querySelector('.fullscreen-btn').addEventListener("click", toggleFullscreen) document.addEventListener('fullscreenchange', event => { if (!document.fullscreenElement) { screen.orientation.unlock() that.classList.remove('fullscreen') that.querySelector('.fullscreen-btn').innerHTML = icon.enterFullscreen } else { self.controls.querySelector('.fullscreen-btn').innerHTML = icon.exitFullscreen } }) that.append(self.controls) } function getVideoWidth() { if (document.fullscreenElement) { return '' } var av = that.querySelector('video.active') return av ? av.getBoundingClientRect().width + 'px' : '100%' } var playOnLoad = false var unblock = document.createElement("div") that.addEventListener("requiresusergesture", event => { unblock.style.position = "absolute" unblock.style.width = '100%' unblock.style.height = '100%' unblock.style.backgroundImage = `url(${self.options.poster})` unblock.style.zIndex = '1000' unblock.style.backgroundPosition = "top left" unblock.style.backgroundRepeat = "no-repeat" unblock.style.backgroundSize = "cover" unblock.style.display = 'flex' unblock.classList.add('mx-controls') unblock.classList.add('poster') unblock.innerHTML = `
${icon.play}
` self.controls.style.opacity = '0' unblock.addEventListener("click", event => { event.preventDefault() event.stopPropagation() playOnLoad = true unblock.querySelector('.toggle').innerHTML = `
${icon.loading}
` }, {once: true}) that.append(unblock) }) var loading = true var touching = false that.brightness(0) that.addEventListener("loadedmetadata", event => { // }) that.addEventListener("seeked", event => { if (loading) { that.brightness(1) loading = false } if (playOnLoad) { playOnLoad = false var toggle = self.controls.querySelector('.toggle') toggle.title = 'Pause' toggle.querySelector('div').innerHTML = icon.pause self.controls.style.opacity = '0' unblock.remove() self.controls.classList.remove('poster') that.play() } }) var time = that.querySelector('.controls .time div'); const progressbar = that.querySelector('.seekbar div[role="progressbar"]'); function setProgressPosition(value) { progressbar.style.width = value + '%'; progressbar.setAttribute('aria-valuenow', value); } that.querySelector('.controls .position input').addEventListener('input', event => { event.preventDefault() event.stopPropagation() setProgressPosition(event.target.value) var position = event.target.value/100 * self.options.duration displayTime(position) that.currentTime(position) hideControlsLater() }) function displayTime(currentTime) { duration = formatDuration(self.options.duration) currentTime = formatDuration(currentTime) while (duration && duration.startsWith('00:')) { duration = duration.slice(3) } currentTime = currentTime.slice(currentTime.length - duration.length) time.innerText = `${currentTime} / ${duration}` } that.addEventListener("timeupdate", event => { var currentTime = that.currentTime(), duration = self.options.duration if (self.options.position) { currentTime -= self.options.position } setProgressPosition(100 * currentTime / duration) displayTime(currentTime) }) that.addEventListener("play", event => { var toggle = self.controls.querySelector('.toggle') toggle.title = 'Pause' toggle.querySelector('div').innerHTML = icon.pause self.controls.style.opacity = '0' }) that.addEventListener("pause", event => { var toggle = self.controls.querySelector('.toggle') toggle.title = 'Play' toggle.querySelector('div').innerHTML = icon.play self.controls.style.opacity = '1' }) that.addEventListener("ended", event => { var toggle = self.controls.querySelector('.toggle') toggle.title = 'Play' toggle.querySelector('div').innerHTML = icon.play self.controls.style.opacity = '1' }) that.addEventListener("seeking", event => { //console.log("seeking") }) that.addEventListener("seeked", event => { //console.log("seeked") }) that.addEventListener("volumechange", event => { var volume = self.controls.querySelector('.volume') if (that.muted()) { volume.innerHTML = icon.unmute volume.title = "Unmute" } else { volume.innerHTML = icon.mute volume.title = "Mute" } }) window.addEventListener('resize', event => { // }) return that }; })();