import { tag, WeElement, h } from 'omi' import { domReady } from '../util/dom-ready' import * as css from './index.scss' import '../theme.ts' interface Props { src: string type?: string } interface Data { } @tag('m-player') export default class Player extends WeElement { video: any disX: any timer: any Hls: any static css = css static propTypes = { src: String, type: String } $(node) { let dom = this.shadowRoot.querySelectorAll(node) return dom.length > 1 ? dom : dom[0] } dom() { this.video = this.$('video') this.video.volume = 0.5 setVolume(this.video.volume * 10, this.$('.line')) this.$('.is-volume').onclick = () => this.volume() this.$('.line').forEach((item, index) => { item.onclick = () => { this.video.volume = (index + 1) / 10 setVolume(index + 1, this.$('.line')) } }) this.$('.progress').onmousedown = e => this.progress(e) this.video.onwaiting = () => this.waiting() this.video.oncanplay = () => this.canplay() this.video.ontimeupdate = () => this.updating() this.$('.cycle').onmousedown = e => this.down(e) this.$('.eplayer').onmousemove = () => this.alow() document.onkeydown = e => this.keydown(e) this.$('.ep-full').onclick = () => this.full() this.$('.ep-video').onclick = this.$('.is-play').onclick = () => this.play() this.video.onended = () => this.ended() this.$('.mark').ondblclick = () => { clearTimeout(this.timer) this.full() } } waiting() { this.$('.mark').classList.remove('playing') this.$('.mark').classList.add('loading') } totalTime: string = '00:00' canplay() { this.$('.mark').classList.remove('loading') this.$('.mark').classList.add('playing') this.$('.playing').onclick = () => { clearTimeout(this.timer) this.timer = setTimeout(() => { this.play() }, 200) } this.$('.total').innerHTML = getTimeStr(this.video.duration) this.totalTime = getTimeStr(this.video.duration) this.$('.total').innerHTML = this.totalTime } isPlaying: boolean play() { if (this.video.paused) { this.video.play() this.isPlaying = true this.$('.ep-video').style.display = 'none' } else { this.video.pause() this.isPlaying = false this.$('.ep-video').style.display = 'block' } this.update() } muted: boolean = false volume() { if (this.video.muted) { this.video.muted = false this.muted = false setVolume(this.video.volume * 10, this.$('.line')) } else { this.muted = true this.video.muted = true setVolume(0, this.$('.line')) } this.update() } updating() { let cTime = getTimeStr(this.video.currentTime) if (this.video.buffered.length) { let bufferEnd = this.video.buffered.end(this.video.buffered.length - 1) this.$('.buffer').style.width = (bufferEnd / this.video.duration) * this.$('.progress').clientWidth + 'px' } let offset = (this.video.currentTime / this.video.duration) * this.$('.bg').clientWidth this.$('.now').innerHTML = cTime this.$('.current').style.width = offset + 'px' } progress(e) { let offset = e.offsetX / this.$('.progress').offsetWidth this.video.currentTime = this.video.duration * offset } down(e) { e.stopPropagation() this.disX = e.clientX - this.$('.cycle').offsetLeft document.onmousemove = e => this.move(e) document.onmouseup = () => { e.stopPropagation() document.onmousemove = null document.onmouseup = null } } move(e) { e.stopPropagation() let offset = e.clientX - this.disX + 7 if (offset < 0) offset = 0 if (offset > this.$('.progress').clientWidth) { offset = this.$('.progress').clientWidth } this.$('.current').style.width = offset + 'px' this.video.currentTime = (offset / this.$('.progress').clientWidth) * this.video.duration document.onmousemove = null setTimeout( (document.onmousemove = e => { if (e) this.move(e) }), 30 ) } alow() { clearTimeout(this.timer) this.$('.controls').style.bottom = 0 this.$('.ep-video').style.bottom = 70 + 'px' this.timer = setTimeout(() => { this.$('.controls').style.bottom = -50 + 'px' this.$('.ep-video').style.bottom = 25 + 'px' }, 5000) } keydown(e) { switch (e.keyCode) { case 37: this.video.currentTime -= 10 break case 39: this.video.currentTime += 10 break case 38: setVolume(this.video.volume.toFixed(1) * 10, this.$('.line')) break case 40: setVolume(this.video.volume.toFixed(1) * 10, this.$('.line')) break case 32: this.play() break default: } } ended() { this.isPlaying = false this.update() } full() { if (isFullScreen()) { document.exitFullscreen() } else { let el = this.$('.eplayer') let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen return rfs.call(el) } } installed() { domReady(() => { this.update() this.dom() }) } render(props) { // this.stream(props.src, props.type) return (