import { LitElement, css, html } from 'lit' import './RangeSlider.js' import './SvgIcon.js' import { formatSeconds } from '../api/utils.js' class VideoPlayer extends LitElement { static properties = { media: { type: String }, autoplay: { type: Boolean }, videoEl: { state: true }, playing: { state: true }, controls: { state: true }, ctrlTimer: { state: true }, position: { state: true }, duration: { state: true }, remain: { state: true } } constructor() { super() this.media = '' this.hostHeight = 0 this.hostWidth = 0 this.playing = false this.controls = true this.ctrlTimer = {} this.position = 0 this.duration = 0 this.remain = true this.autoplay = false this.videoEl = document.createElement('video') this.videoEl.playsInline = true this.videoEl.preload = 'metadata' } firstUpdated() { this.videoEl.addEventListener('loadeddata', () => { this.duration = this.videoEl.duration }) this.videoEl.addEventListener('timeupdate', () => { this.position = this.videoEl.currentTime }) } willUpdate(att) { if (att.has('media')) { this.videoEl.src = this.media if (this.autoplay) { this.togglePlay() this.interact() } } this.update() } togglePlay() { if (this.playing) { this.videoEl.pause() } else { this.videoEl.play() } this.playing = !this.playing } interact() { if (this.playing) { // show controls this.controls = true clearTimeout(this.ctrlTimer) // timeout this.ctrlTimer = setTimeout(() => { if (this.playing) { this.controls = false } }, 2000) } } toggleFullscreen() { const container = this.shadowRoot.querySelector('.videoContainer') if (container.webkitSupportsFullscreen) { container.webkitEnterFullscreen() return } if (document.fullscreenElement !== null) { document.exitFullscreen() container.setAttribute('data-fullscreen', false) } else { container.requestFullscreen() container.setAttribute('data-fullscreen', true) } } toggleDuration() { console.log(this.remain) this.remain = !this.remain } seek({ detail }) { if (detail) { this.interact() this.videoEl.currentTime = detail.value this.position = detail.value } } render() { return html`