create-video-context.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5
import {
  invokeMethod,
  getCurrentPageVm
} from '../../platform'

6
const RATES = [0.5, 0.8, 1.0, 1.25, 1.5, 2.0]
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

function operateVideoPlayer (videoId, pageVm, type, data) {
  invokeMethod('operateVideoPlayer', videoId, pageVm, type, data)
}

class VideoContext {
  constructor (id, pageVm) {
    this.id = id
    this.pageVm = pageVm
  }

  play () {
    operateVideoPlayer(this.id, this.pageVm, 'play')
  }
  pause () {
    operateVideoPlayer(this.id, this.pageVm, 'pause')
  }
  stop () {
    operateVideoPlayer(this.id, this.pageVm, 'stop')
  }
  seek (position) {
d-u-a's avatar
d-u-a 已提交
28 29 30
    operateVideoPlayer(this.id, this.pageVm, 'seek', {
      position
    })
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38
  }
  sendDanmu (args) {
    operateVideoPlayer(this.id, this.pageVm, 'sendDanmu', args)
  }
  playbackRate (rate) {
    if (!~RATES.indexOf(rate)) {
      rate = 1.0
    }
d-u-a's avatar
d-u-a 已提交
39 40 41
    operateVideoPlayer(this.id, this.pageVm, 'playbackRate', {
      rate
    })
fxy060608's avatar
fxy060608 已提交
42
  }
d-u-a's avatar
d-u-a 已提交
43
  requestFullScreen (args = {}) {
44
    operateVideoPlayer(this.id, this.pageVm, 'requestFullScreen', args)
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }
  exitFullScreen () {
    operateVideoPlayer(this.id, this.pageVm, 'exitFullScreen')
  }
  showStatusBar () {
    operateVideoPlayer(this.id, this.pageVm, 'showStatusBar')
  }
  hideStatusBar () {
    operateVideoPlayer(this.id, this.pageVm, 'hideStatusBar')
  }
}

export function createVideoContext (id, context) {
  if (context) {
    return new VideoContext(id, context)
  }
  return new VideoContext(id, getCurrentPageVm('createVideoContext'))
}