index.js 2.01 KB
let id = 0

class videoEnPlayer extends Emitter {
  constructor(options) {
    super()

    this.options = options.config
    this.callBackFun = options.callBackFun

    this.id = ++id
    this.player = null
    this.poster = ''
    this.previewPoster = ''
    this.originPoster = ''
    this.isDestroy = false
    this.container = document.querySelector(`#video-player-${this.options.key}`)

    this.initOhtersPlayer()

    this.template = new Template(this)
    this.events = new Events(this)
  }

  get proxy() {
    return this.events.proxy
  }

  get query() {
    return this.template.query
  }

  initOhtersPlayer() {
    const domId = this.container.getAttribute('id')
    const url = this.options ? this.options.record ? this.options.record.url : '' : ''
    const isDark = document.querySelector('html').getAttribute('dark-mode') === 'true'
    this.originPoster = this.options.record.poster
    this.previewPoster = this.options.record.poster

    const cdnUrl = [
      'cdnjdcontent.aikan.pdnews.cn',
      'jdcontentcdn.aikan.pdnews.cn',
      'sitjdcontentcdn.aikan.pdnews.cn',
      'uatjdcdncollect.aikan.pdnews.cn',
      'uatjdcontentcdn.aikan.pdnews.cn'
    ]

    if (!this.previewPoster) {
      this.originPoster = `${url.split('?')[0]}?x-oss-process=video/snapshot,t_0,f_jpg`
      this.previewPoster = `${url.split('?')[0]}?x-oss-process=video/snapshot,t_0,f_jpg`
    } else {
      const isNewspaper = this.options.record.isNewspaper
      this.originPoster = handleImageSrc(this.originPoster, 2, isNewspaper)
      this.previewPoster = handleImageSrc(this.previewPoster, 2, isNewspaper)
    }

    this.poster = isDark ? './image/placeHoldVlogo.svg' : './image/placeholdLogo.svg'

    this.player = new Aliplayer({
      id: domId,
      autoplay: this.options.isGiveWidth,
      x5_type: true,
      source: url
    })
  }

  destroy() {
    this.poster = ''
    this.originPoster = ''
    this.isDestroy = true
    this.events.destroy()

    if (this.player) {
      this.player.dispose()
    }

    this.player = null
  }
}