index.vue 30.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
<template>
  <uni-video
    :id="id"
4
    v-on="$listeners"
fxy060608's avatar
fxy060608 已提交
5
  >
fxy060608's avatar
fxy060608 已提交
6 7 8 9
    <div
      ref="container"
      class="uni-video-container"
      @click="triggerControls"
Q
qiang 已提交
10 11 12 13 14
      @touchstart="touchstart"
      @touchend="touchend"
      @touchmove="touchmove"
      @fullscreenchange.stop="onFullscreenChange"
      @webkitfullscreenchange.stop="onFullscreenChange($event,true)"
15
    >
fxy060608's avatar
fxy060608 已提交
16 17
      <video
        ref="video"
18
        :style="{objectFit:objectFit}"
fxy060608's avatar
fxy060608 已提交
19 20
        :muted="muted"
        :loop="loop"
21
        :src="srcSync"
fxy060608's avatar
fxy060608 已提交
22
        :poster="poster"
Q
qiang 已提交
23
        :autoplay="autoplay"
Q
qiang 已提交
24
        v-bind="$attrs"
fxy060608's avatar
fxy060608 已提交
25 26 27
        class="uni-video-video"
        webkit-playsinline
        playsinline
Q
qiang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
        @durationchange="onDurationChange"
        @loadedmetadata="onLoadedMetadata"
        @progress="onProgress"
        @waiting="onWaiting"
        @error="onVideoError"
        @play="onPlay"
        @pause="onPause"
        @ended="onEnded"
        @timeupdate="onTimeUpdate"
        @webkitbeginfullscreen="emitFullscreenChange(true)"
        @x5videoenterfullscreen="emitFullscreenChange(true)"
        @webkitendfullscreen="emitFullscreenChange(false)"
        @x5videoexitfullscreen="emitFullscreenChange(false)"
41
      />
42 43 44
      <div
        v-show="controlsShow"
        class="uni-video-bar uni-video-bar-full"
45
        @click.stop
fxy060608's avatar
fxy060608 已提交
46
      >
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51
        <div class="uni-video-controls">
          <div
            v-show="showPlayBtn"
            :class="{'uni-video-control-button-play':!playing,'uni-video-control-button-pause':playing}"
            class="uni-video-control-button"
52 53
            @click.stop="trigger"
          />
54 55
          <div class="uni-video-current-time">
            {{ currentTime|time }}
fxy060608's avatar
fxy060608 已提交
56
          </div>
fxy060608's avatar
fxy060608 已提交
57 58 59
          <div
            ref="progress"
            class="uni-video-progress-container"
60 61
            @click.stop="clickProgress($event)"
          >
fxy060608's avatar
fxy060608 已提交
62
            <div class="uni-video-progress">
63
              <div
Q
qiang 已提交
64
                :style="{width:buffered+'%'}"
65
                class="uni-video-progress-buffered"
fxy060608's avatar
fxy060608 已提交
66
              />
67 68 69
              <div
                ref="ball"
                :style="{left:progress+'%'}"
70
                class="uni-video-ball"
fxy060608's avatar
fxy060608 已提交
71
              >
Q
qiang 已提交
72
                <div class="uni-video-inner" />
fxy060608's avatar
fxy060608 已提交
73 74 75
              </div>
            </div>
          </div>
76 77
          <div class="uni-video-duration">
            {{ (duration||durationTime)|time }}
fxy060608's avatar
fxy060608 已提交
78
          </div>
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83
        </div>
        <div
          v-if="danmuBtn"
          :class="{'uni-video-danmu-button-active':enableDanmuSync}"
          class="uni-video-danmu-button"
84
          @click.stop="triggerDanmu"
85 86
        >
          弹幕
fxy060608's avatar
fxy060608 已提交
87
        </div>
fxy060608's avatar
fxy060608 已提交
88 89 90 91
        <div
          v-show="showFullscreenBtn"
          :class="{'uni-video-type-fullscreen':fullscreen}"
          class="uni-video-fullscreen"
Q
qiang 已提交
92
          @click.stop="triggerFullscreen(!fullscreen)"
93
        />
fxy060608's avatar
fxy060608 已提交
94
      </div>
95 96 97 98
      <div
        v-show="start&&enableDanmuSync"
        ref="danmu"
        style="z-index: 0;"
99
        class="uni-video-danmu"
fxy060608's avatar
fxy060608 已提交
100
      />
101
      <div
102
        v-if="centerPlayBtnShow"
103
        class="uni-video-cover"
104
        @click.stop
fxy060608's avatar
fxy060608 已提交
105
      >
106 107
        <div
          class="uni-video-cover-play-button"
108
          @click.stop="play"
fxy060608's avatar
fxy060608 已提交
109
        />
110 111
        <p class="uni-video-cover-duration">
          {{ (duration||durationTime)|time }}
fxy060608's avatar
fxy060608 已提交
112
        </p>
fxy060608's avatar
fxy060608 已提交
113
      </div>
114 115
      <div
        :class="{'uni-video-toast-volume':gestureType==='volume'}"
116
        class="uni-video-toast"
fxy060608's avatar
fxy060608 已提交
117
      >
118 119
        <div class="uni-video-toast-title">
          音量
fxy060608's avatar
fxy060608 已提交
120
        </div>
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126
        <svg
          class="uni-video-toast-icon"
          width="200px"
          height="200px"
          viewBox="0 0 1024 1024"
          version="1.1"
127 128 129 130 131
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M475.400704 201.19552l0 621.674496q0 14.856192-10.856448 25.71264t-25.71264 10.856448-25.71264-10.856448l-190.273536-190.273536-149.704704 0q-14.856192 0-25.71264-10.856448t-10.856448-25.71264l0-219.414528q0-14.856192 10.856448-25.71264t25.71264-10.856448l149.704704 0 190.273536-190.273536q10.856448-10.856448 25.71264-10.856448t25.71264 10.856448 10.856448 25.71264zm219.414528 310.837248q0 43.425792-24.28416 80.851968t-64.2816 53.425152q-5.71392 2.85696-14.2848 2.85696-14.856192 0-25.71264-10.570752t-10.856448-25.998336q0-11.999232 6.856704-20.284416t16.570368-14.2848 19.427328-13.142016 16.570368-20.284416 6.856704-32.569344-6.856704-32.569344-16.570368-20.284416-19.427328-13.142016-16.570368-14.2848-6.856704-20.284416q0-15.427584 10.856448-25.998336t25.71264-10.570752q8.57088 0 14.2848 2.85696 39.99744 15.427584 64.2816 53.139456t24.28416 81.137664zm146.276352 0q0 87.422976-48.56832 161.41824t-128.5632 107.707392q-7.428096 2.85696-14.2848 2.85696-15.427584 0-26.284032-10.856448t-10.856448-25.71264q0-22.284288 22.284288-33.712128 31.997952-16.570368 43.425792-25.141248 42.283008-30.855168 65.995776-77.423616t23.712768-99.136512-23.712768-99.136512-65.995776-77.423616q-11.42784-8.57088-43.425792-25.141248-22.284288-11.42784-22.284288-33.712128 0-14.856192 10.856448-25.71264t25.71264-10.856448q7.428096 0 14.856192 2.85696 79.99488 33.712128 128.5632 107.707392t48.56832 161.41824zm146.276352 0q0 131.42016-72.566784 241.41312t-193.130496 161.989632q-7.428096 2.85696-14.856192 2.85696-14.856192 0-25.71264-10.856448t-10.856448-25.71264q0-20.570112 22.284288-33.712128 3.999744-2.285568 12.85632-5.999616t12.85632-5.999616q26.284032-14.2848 46.854144-29.140992 70.281216-51.996672 109.707264-129.705984t39.426048-165.132288-39.426048-165.132288-109.707264-129.705984q-20.570112-14.856192-46.854144-29.140992-3.999744-2.285568-12.85632-5.999616t-12.85632-5.999616q-22.284288-13.142016-22.284288-33.712128 0-14.856192 10.856448-25.71264t25.71264-10.856448q7.428096 0 14.856192 2.85696 120.563712 51.996672 193.130496 161.989632t72.566784 241.41312z"
          />
fxy060608's avatar
fxy060608 已提交
132 133
        </svg>
        <div class="uni-video-toast-value">
134 135
          <div
            :style="{width:volumeNew*100+'%'}"
136
            class="uni-video-toast-value-content"
fxy060608's avatar
fxy060608 已提交
137
          >
fxy060608's avatar
fxy060608 已提交
138 139 140 141
            <div class="uni-video-toast-volume-grids">
              <div
                v-for="(item,index) in 10"
                :key="index"
142 143
                class="uni-video-toast-volume-grids-item"
              />
fxy060608's avatar
fxy060608 已提交
144 145 146 147
            </div>
          </div>
        </div>
      </div>
148 149
      <div
        :class="{'uni-video-toast-progress':gestureType=='progress'}"
150
        class="uni-video-toast"
fxy060608's avatar
fxy060608 已提交
151
      >
152 153
        <div class="uni-video-toast-title">
          {{ currentTimeNew|time }} / {{ durationTime|time }}
fxy060608's avatar
fxy060608 已提交
154
        </div>
fxy060608's avatar
fxy060608 已提交
155 156
      </div>
    </div>
157 158 159
    <div
      style="position: absolute; top: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none;"
    >
Q
qiang 已提交
160
      <slot />
郭胜强 已提交
161
    </div>
fxy060608's avatar
fxy060608 已提交
162 163 164 165
  </uni-video>
</template>
<script>
import {
Q
qiang 已提交
166 167
  subscriber,
  interact
fxy060608's avatar
fxy060608 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
} from 'uni-mixins'
import {
  supportsPassive
} from 'uni-shared'

const passiveOptions = supportsPassive ? {
  passive: false
} : false

const GestureType = {
  NONE: 'none',
  STOP: 'stop',
  VOLUME: 'volume',
  PROGRESS: 'progress'
}

export default {
  name: 'Video',
  filters: {
Q
qiang 已提交
187 188 189 190 191
    time (val) {
      val = val > 0 && val < Infinity ? val : 0
      let h = Math.floor(val / 3600)
      let m = Math.floor(val % 3600 / 60)
      let s = Math.floor(val % 3600 % 60)
fxy060608's avatar
fxy060608 已提交
192 193 194
      h = (h < 10 ? '0' : '') + h
      m = (m < 10 ? '0' : '') + m
      s = (s < 10 ? '0' : '') + s
Q
qiang 已提交
195
      let str = m + ':' + s
fxy060608's avatar
fxy060608 已提交
196 197 198 199 200 201
      if (h !== '00') {
        str = h + ':' + str
      }
      return str
    }
  },
Q
qiang 已提交
202
  mixins: [subscriber, interact],
fxy060608's avatar
fxy060608 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  props: {
    id: {
      type: String,
      default: ''
    },
    src: {
      type: String,
      default: ''
    },
    duration: {
      type: [Number, String],
      default: ''
    },
    controls: {
      type: [Boolean, String],
      default: true
    },
    danmuList: {
      type: Array,
      default () {
        return []
      }
    },
    danmuBtn: {
      type: [Boolean, String],
      default: false
    },
    enableDanmu: {
      type: [Boolean, String],
      default: false
    },
    autoplay: {
      type: [Boolean, String],
      default: false
    },
    loop: {
      type: [Boolean, String],
      default: false
    },
    muted: {
      type: [Boolean, String],
      default: false
    },
    objectFit: {
      type: String,
      default: 'contain'
    },
    poster: {
      type: String,
      default: ''
    },
    direction: {
      type: [String, Number],
Q
qiang 已提交
256
      default: ''
fxy060608's avatar
fxy060608 已提交
257
    },
258 259 260 261
    showProgress: {
      type: Boolean,
      default: true
    },
fxy060608's avatar
fxy060608 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    initialTime: {
      type: [String, Number],
      default: 0
    },
    showFullscreenBtn: {
      type: [Boolean, String],
      default: true
    },
    pageGesture: {
      type: [Boolean, String],
      default: false
    },
    enableProgressGesture: {
      type: [Boolean, String],
      default: true
    },
    showPlayBtn: {
      type: [Boolean, String],
      default: true
281 282 283 284
    },
    showCenterPlayBtn: {
      type: [Boolean, String],
      default: true
fxy060608's avatar
fxy060608 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    }
  },
  data () {
    return {
      start: false,
      playing: false,
      currentTime: 0,
      durationTime: 0,
      progress: 0,
      touching: false,
      enableDanmuSync: Boolean(this.enableDanmu),
      controlsVisible: true,
      fullscreen: false,
      controlsTouching: false,
      touchStartOrigin: {
        x: 0,
        y: 0
      },
      gestureType: GestureType.NONE,
      currentTimeOld: 0,
      currentTimeNew: 0,
      volumeOld: null,
      volumeNew: null,
      buffered: 0,
Q
qiang 已提交
309
      isSafari: /^Apple/.test(navigator.vendor)
fxy060608's avatar
fxy060608 已提交
310 311 312
    }
  },
  computed: {
313
    centerPlayBtnShow () {
314
      return this.showCenterPlayBtn && !this.start
315
    },
fxy060608's avatar
fxy060608 已提交
316
    controlsShow () {
317
      return !this.centerPlayBtnShow && this.controls && this.controlsVisible
fxy060608's avatar
fxy060608 已提交
318 319 320
    },
    autoHideContorls () {
      return this.controlsShow && this.playing && !this.controlsTouching
321 322 323
    },
    srcSync () {
      return this.$getRealPath(this.src)
fxy060608's avatar
fxy060608 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336
    }
  },
  watch: {
    enableDanmuSync (val) {
      this.$emit('update:enableDanmu', val)
    },
    autoHideContorls (val) {
      if (val) {
        this.autoHideStart()
      } else {
        this.autoHideEnd()
      }
    },
337
    srcSync (val) {
fxy060608's avatar
fxy060608 已提交
338 339 340 341 342 343 344 345
      this.playing = false
      this.currentTime = 0
    },
    currentTime () {
      this.updateProgress()
    },
    duration () {
      this.updateProgress()
Q
qiang 已提交
346 347 348 349 350 351 352
    },
    buffered (buffered) {
      if (buffered !== 0) {
        this.$trigger('progress', {}, {
          buffered
        })
      }
fxy060608's avatar
fxy060608 已提交
353 354 355 356 357 358 359 360 361 362 363
    }
  },
  created () {
    this.otherData = {
      danmuList: [],
      danmuIndex: {
        time: 0,
        index: -1
      },
      hideTiming: null
    }
Q
qiang 已提交
364
    const danmuList = this.otherData.danmuList = JSON.parse(JSON.stringify(this.danmuList || []))
fxy060608's avatar
fxy060608 已提交
365 366 367 368 369
    danmuList.sort(function (a, b) {
      return (a.time || 0) - (a.time || 0)
    })
  },
  mounted () {
Q
qiang 已提交
370 371 372 373 374 375 376 377 378
    const self = this
    let originX
    let originY
    let moveOnce = true
    let originProgress
    const ball = this.$refs.ball
    ball.addEventListener('touchstart', (event) => {
      this.controlsTouching = true
      const toucher = event.targetTouches[0]
fxy060608's avatar
fxy060608 已提交
379 380
      originX = toucher.pageX
      originY = toucher.pageY
Q
qiang 已提交
381
      originProgress = this.progress
fxy060608's avatar
fxy060608 已提交
382
      moveOnce = true
Q
qiang 已提交
383
      this.touching = true
fxy060608's avatar
fxy060608 已提交
384 385 386 387
      ball.addEventListener('touchmove', touchmove, passiveOptions)
    })

    function touchmove (event) {
Q
qiang 已提交
388 389 390
      const toucher = event.targetTouches[0]
      const pageX = toucher.pageX
      const pageY = toucher.pageY
fxy060608's avatar
fxy060608 已提交
391 392 393 394 395
      if (moveOnce && Math.abs(pageX - originX) < Math.abs(pageY - originY)) {
        touchend()
        return
      }
      moveOnce = false
Q
qiang 已提交
396 397
      const w = self.$refs.progress.offsetWidth
      let progress = originProgress + (pageX - originX) / w * 100
fxy060608's avatar
fxy060608 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
      if (progress < 0) {
        progress = 0
      } else if (progress > 100) {
        progress = 100
      }
      self.progress = progress
      event.preventDefault()
      event.stopPropagation()
    }

    function touchend (event) {
      self.controlsTouching = false
      if (self.touching) {
        ball.removeEventListener('touchmove', touchmove, passiveOptions)
        if (!moveOnce) {
          event.preventDefault()
          event.stopPropagation()
          self.seek(self.$refs.video.duration * self.progress / 100)
        }
        self.touching = false
      }
    }
    ball.addEventListener('touchend', touchend)
    ball.addEventListener('touchcancel', touchend)
  },
  beforeDestroy () {
Q
qiang 已提交
424
    this.triggerFullscreen(false)
fxy060608's avatar
fxy060608 已提交
425 426 427 428 429 430 431
    clearTimeout(this.otherData.hideTiming)
  },
  methods: {
    _handleSubscribe ({
      type,
      data = {}
    }) {
Q
qiang 已提交
432 433
      const methods = ['play', 'pause', 'seek', 'sendDanmu', 'playbackRate', 'requestFullScreen', 'exitFullScreen']
      let options
fxy060608's avatar
fxy060608 已提交
434 435
      switch (type) {
        case 'seek':
Q
qiang 已提交
436
          options = data.position
fxy060608's avatar
fxy060608 已提交
437 438
          break
        case 'sendDanmu':
Q
qiang 已提交
439
          options = data
fxy060608's avatar
fxy060608 已提交
440 441
          break
        case 'playbackRate':
Q
qiang 已提交
442
          options = data.rate
fxy060608's avatar
fxy060608 已提交
443 444
          break
      }
Q
qiang 已提交
445 446
      if (methods.indexOf(type) >= 0) {
        this[type](options)
fxy060608's avatar
fxy060608 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
      }
    },
    trigger () {
      if (this.playing) {
        this.$refs.video.pause()
      } else {
        this.$refs.video.play()
      }
    },
    play () {
      this.start = true
      this.$refs.video.play()
    },
    pause () {
      this.$refs.video.pause()
    },
    seek (position) {
      position = Number(position)
      if (typeof position === 'number' && !isNaN(position)) {
        this.$refs.video.currentTime = position
      }
    },
    clickProgress (event) {
Q
qiang 已提交
470 471 472 473
      const $progress = this.$refs.progress
      let element = event.target
      let x = event.offsetX
      while (element !== $progress) {
fxy060608's avatar
fxy060608 已提交
474 475 476
        x += element.offsetLeft
        element = element.parentNode
      }
Q
qiang 已提交
477 478
      const w = $progress.offsetWidth
      let progress = 0
fxy060608's avatar
fxy060608 已提交
479 480 481 482 483 484 485 486 487
      if (x >= 0 && x <= w) {
        progress = x / w
        this.seek(this.$refs.video.duration * progress)
      }
    },
    triggerDanmu () {
      this.enableDanmuSync = !this.enableDanmuSync
    },
    playDanmu (danmu) {
Q
qiang 已提交
488
      const p = document.createElement('p')
fxy060608's avatar
fxy060608 已提交
489 490
      p.className = 'uni-video-danmu-item'
      p.innerText = danmu.text
Q
qiang 已提交
491
      let style = `bottom: ${Math.random() * 100}%;color: ${danmu.color};`
fxy060608's avatar
fxy060608 已提交
492 493 494 495 496 497 498 499 500 501 502
      p.setAttribute('style', style)
      this.$refs.danmu.appendChild(p)
      setTimeout(function () {
        style += 'left: 0;-webkit-transform: translateX(-100%);transform: translateX(-100%);'
        p.setAttribute('style', style)
        setTimeout(function () {
          p.remove()
        }, 4000)
      }, 17)
    },
    sendDanmu (danmu) {
Q
qiang 已提交
503
      const otherData = this.otherData
fxy060608's avatar
fxy060608 已提交
504 505 506 507 508 509
      otherData.danmuList.splice(otherData.danmuIndex.index + 1, 0, {
        text: String(danmu.text),
        color: danmu.color,
        time: this.$refs.video.currentTime || 0
      })
    },
Q
qiang 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    playbackRate (rate) {
      this.$refs.video.playbackRate = rate
    },
    triggerFullscreen (val) {
      const container = this.$refs.container
      const video = this.$refs.video
      let mockFullScreen
      if (val) {
        if ((document.fullscreenEnabled || document.webkitFullscreenEnabled) && (!this.isSafari || this.userInteract)) {
          container[document.fullscreenEnabled ? 'requestFullscreen' : 'webkitRequestFullscreen']()
        } else if (video.webkitEnterFullScreen) {
          video.webkitEnterFullScreen()
        } else {
          mockFullScreen = true
          container.remove()
          container.classList.add('uni-video-type-fullscreen')
          document.body.appendChild(container)
        }
      } else {
        if (document.fullscreenEnabled || document.webkitFullscreenEnabled) {
530 531 532 533 534
          if (document.fullscreenElement) {
            document.exitFullscreen()
          } else if (document.webkitFullscreenElement) {
            document.webkitExitFullscreen()
          }
Q
qiang 已提交
535 536 537 538 539 540 541 542 543 544 545 546
        } else if (video.webkitExitFullScreen) {
          video.webkitExitFullScreen()
        } else {
          mockFullScreen = true
          container.remove()
          container.classList.remove('uni-video-type-fullscreen')
          this.$el.appendChild(container)
        }
      }
      if (mockFullScreen) {
        this.emitFullscreenChange(val)
      }
fxy060608's avatar
fxy060608 已提交
547
    },
Q
qiang 已提交
548 549 550
    onFullscreenChange ($event, webkit) {
      if (webkit && document.fullscreenEnabled) {
        return
fxy060608's avatar
fxy060608 已提交
551
      }
Q
qiang 已提交
552 553 554 555 556 557 558 559
      this.emitFullscreenChange(!!(document.fullscreenElement || document.webkitFullscreenElement))
    },
    emitFullscreenChange (val) {
      this.fullscreen = val
      this.$trigger('fullscreenchange', {}, {
        fullScreen: val,
        direction: 'vertical'
      })
fxy060608's avatar
fxy060608 已提交
560
    },
Q
qiang 已提交
561 562 563 564 565 566 567 568 569
    requestFullScreen () {
      this.triggerFullscreen(true)
    },
    exitFullScreen () {
      this.triggerFullscreen(false)
    },
    onDurationChange ({ target }) {
      this.durationTime = target.duration
    },
570
    onLoadedMetadata ($event) {
Q
qiang 已提交
571
      const initialTime = Number(this.initialTime) || 0
572
      const video = $event.target
Q
qiang 已提交
573
      if (initialTime > 0) {
574
        video.currentTime = initialTime
Q
qiang 已提交
575
      }
576 577 578 579 580
      this.$trigger('loadedmetadata', $event, {
        width: video.videoWidth,
        height: video.videoHeight,
        duration: video.duration
      })
Q
qiang 已提交
581
      this.onProgress($event)
Q
qiang 已提交
582
    },
Q
qiang 已提交
583 584 585
    onProgress ($event) {
      const video = $event.target
      const buffered = video.buffered
Q
qiang 已提交
586
      if (buffered.length) {
Q
qiang 已提交
587
        this.buffered = buffered.end(buffered.length - 1) / video.duration * 100
Q
qiang 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
      }
    },
    onWaiting ($event) {
      this.$trigger('waiting', $event, {})
    },
    onVideoError ($event) {
      this.playing = false
      this.$trigger('error', $event, {})
    },
    onPlay ($event) {
      this.start = true
      this.playing = true
      this.$trigger('play', $event, {})
    },
    onPause ($event) {
      this.playing = false
      this.$trigger('pause', $event, {})
    },
    onEnded ($event) {
      this.playing = false
      this.$trigger('ended', $event, {})
    },
    onTimeUpdate ($event) {
      const video = $event.target
      const otherData = this.otherData
      const currentTime = this.currentTime = video.currentTime
      const oldDanmuIndex = otherData.danmuIndex
      const danmuIndex = {
        time: currentTime,
        index: oldDanmuIndex.index
      }
      const danmuList = otherData.danmuList
      if (currentTime > oldDanmuIndex.time) {
        for (let index = oldDanmuIndex.index + 1; index < danmuList.length; index++) {
fxy060608's avatar
fxy060608 已提交
622
          const element = danmuList[index]
Q
qiang 已提交
623 624 625 626 627 628 629 630 631 632 633
          if (currentTime >= (element.time || 0)) {
            danmuIndex.index = index
            if (this.playing && this.enableDanmuSync) {
              this.playDanmu(element)
            }
          } else {
            break
          }
        }
      } else if (currentTime < oldDanmuIndex.time) {
        for (let index = oldDanmuIndex.index - 1; index > -1; index--) {
fxy060608's avatar
fxy060608 已提交
634
          const element = danmuList[index]
Q
qiang 已提交
635 636 637 638 639 640 641 642 643 644 645 646
          if (currentTime <= (element.time || 0)) {
            danmuIndex.index = index - 1
          } else {
            break
          }
        }
      }
      otherData.danmuIndex = danmuIndex
      this.$trigger('timeupdate', $event, {
        currentTime,
        duration: video.duration
      })
fxy060608's avatar
fxy060608 已提交
647 648 649 650 651
    },
    triggerControls () {
      this.controlsVisible = !this.controlsVisible
    },
    touchstart (event) {
Q
qiang 已提交
652
      const toucher = event.targetTouches[0]
fxy060608's avatar
fxy060608 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
      this.touchStartOrigin = {
        x: toucher.pageX,
        y: toucher.pageY
      }
      this.gestureType = GestureType.NONE
      this.volumeOld = null
      this.currentTimeOld = this.currentTimeNew = 0
    },
    touchmove (event) {
      function stop () {
        event.stopPropagation()
        event.preventDefault()
      }
      if (this.fullscreen) {
        stop()
      }
Q
qiang 已提交
669
      const gestureType = this.gestureType
fxy060608's avatar
fxy060608 已提交
670 671 672
      if (gestureType === GestureType.STOP) {
        return
      }
Q
qiang 已提交
673 674 675 676
      const toucher = event.targetTouches[0]
      const pageX = toucher.pageX
      const pageY = toucher.pageY
      const origin = this.touchStartOrigin
fxy060608's avatar
fxy060608 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
      if (gestureType === GestureType.PROGRESS) {
        this.changeProgress(pageX - origin.x)
      } else if (gestureType === GestureType.VOLUME) {
        this.changeVolume(pageY - origin.y)
      }
      if (gestureType !== GestureType.NONE) {
        return
      }
      if (Math.abs(pageX - origin.x) > Math.abs(pageY - origin.y)) {
        if (!this.enableProgressGesture) {
          this.gestureType = GestureType.STOP
          return
        }
        this.gestureType = GestureType.PROGRESS
        this.currentTimeOld = this.currentTimeNew = this.$refs.video.currentTime
        if (!this.fullscreen) {
          stop()
        }
695
      } else {
fxy060608's avatar
fxy060608 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
        if (!this.pageGesture) {
          this.gestureType = GestureType.STOP
          return
        }
        this.gestureType = GestureType.VOLUME
        this.volumeOld = this.$refs.video.volume
        if (!this.fullscreen) {
          stop()
        }
      }
    },
    touchend (event) {
      if (this.gestureType !== GestureType.NONE && this.gestureType !== GestureType.STOP) {
        event.stopPropagation()
        event.preventDefault()
      }
      if (this.gestureType === GestureType.PROGRESS && this.currentTimeOld !== this.currentTimeNew) {
        this.$refs.video.currentTime = this.currentTimeNew
      }
      this.gestureType = GestureType.NONE
    },
    changeProgress (x) {
Q
qiang 已提交
718 719
      const duration = this.$refs.video.duration
      let currentTimeNew = x / 600 * duration + this.currentTimeOld
fxy060608's avatar
fxy060608 已提交
720 721 722 723 724 725 726 727
      if (currentTimeNew < 0) {
        currentTimeNew = 0
      } else if (currentTimeNew > duration) {
        currentTimeNew = duration
      }
      this.currentTimeNew = currentTimeNew
    },
    changeVolume (y) {
Q
qiang 已提交
728 729
      const valueOld = this.volumeOld
      let value
fxy060608's avatar
fxy060608 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      if (typeof valueOld === 'number') {
        value = valueOld - y / 200
        if (value < 0) {
          value = 0
        } else if (value > 1) {
          value = 1
        }
        this.$refs.video.volume = value
        this.volumeNew = value
      }
    },
    autoHideStart () {
      this.otherData.hideTiming = setTimeout(() => {
        this.controlsVisible = false
      }, 3000)
    },
    autoHideEnd () {
Q
qiang 已提交
747
      const otherData = this.otherData
fxy060608's avatar
fxy060608 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
      if (otherData.hideTiming) {
        clearTimeout(otherData.hideTiming)
        otherData.hideTiming = null
      }
    },
    updateProgress () {
      if (!this.touching) {
        this.progress = this.currentTime / this.durationTime * 100
      }
    }
  }
}
</script>

<style>
763
uni-video {
fxy060608's avatar
fxy060608 已提交
764 765 766 767 768 769 770 771
  width: 300px;
  height: 225px;
  display: inline-block;
  line-height: 0;
  overflow: hidden;
  position: relative;
}

772
uni-video[hidden] {
773
  display: none;
774 775
}

fxy060608's avatar
fxy060608 已提交
776 777 778 779 780 781 782 783 784
.uni-video-container {
  width: 100%;
  height: 100%;
  background-color: black;
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
785
  object-position: inherit;
fxy060608's avatar
fxy060608 已提交
786 787 788 789 790 791 792 793 794 795
}

.uni-video-container.uni-video-type-fullscreen {
  position: fixed;
  z-index: 999;
}

.uni-video-video {
  width: 100%;
  height: 100%;
796
  object-position: inherit;
fxy060608's avatar
fxy060608 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
}

.uni-video-cover {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: rgba(1, 1, 1, 0.5);
  z-index: 1;
}

.uni-video-cover-play-button {
  width: 40px;
  height: 40px;
  background-size: 50%;
  background-repeat: no-repeat;
  background-position: 50% 50%;
819
  cursor: pointer;
fxy060608's avatar
fxy060608 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
}

.uni-video-cover-duration {
  color: #fff;
  font-size: 16px;
  line-height: 1;
  margin-top: 10px;
}

.uni-video-bar {
  height: 44px;
  background-color: rgba(0, 0, 0, 0.5);
  overflow: hidden;
  position: absolute;
  bottom: 0;
  right: 0;
  display: flex;
  display: flex;
  align-items: center;
  align-items: center;
  padding: 0 10px;
  z-index: 0;
842 843
  /* 解决全屏后被 video 遮挡的问题 */
  transform: translate3d(0, 0, 0);
fxy060608's avatar
fxy060608 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
}

.uni-video-bar.uni-video-bar-full {
  left: 0;
}

.uni-video-controls {
  display: flex;
  display: flex;
  flex-grow: 1;
  flex-grow: 1;
  margin: 0 8.5px;
}

.uni-video-control-button {
  width: 13px;
  height: 15px;
  padding: 14.5px 12.5px 14.5px 12.5px;
  margin-left: -8.5px;
  box-sizing: content-box;
864
  cursor: pointer;
fxy060608's avatar
fxy060608 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
}

.uni-video-control-button::after {
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  background-size: 100%;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

.uni-video-control-button.uni-video-control-button-play::after,
.uni-video-cover-play-button {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAeCAYAAAAy2w7YAAAAAXNSR0IArs4c6QAAAWhJREFUSA1j+P///0cgBoHjQGzCQCsAtgJB/AMy5wCxGNXtQ9iBwvoA5BUCMQvVLEQxHpNzDSjkRhXLMM3GKrIeKKpEkYVYjcUu+AMo3ALE3GRZiN1MvKKPgbIRJFuG10j8koeA0gZEW4jfLIKyf4EqpgOxMEELCRpFnIJ3QGU5QMyM00LizCFa1SWgSkeslhFtBGkKVwGVy6FYSJp+klR/A6quB2JOkIWMIK0oNlOf8xBoZDE9LAI7nYn6HsBq4l96WHQEaLUpAyiOaASeAM2NgvuPBpaACt82IEYtfKls0UagecpwXyAzqGTRdaA57sjmYrAptAjUsCkGYlYMg9EFyLQI1IiZB8Ti6Obh5JNh0QmgHlOcBuKSIMGi50C18UDMiMssvOJEWPQLqKYbiHnxGkRIkoBF24DyaoTMIEoeh0W3geI+RBlArCI0iz4D+RVAzEasfqLVAQ19AcSg5LoYiKWI1kiiQgCMBLnEEcfDSgAAAABJRU5ErkJggg==");
}

.uni-video-control-button.uni-video-control-button-pause::after {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAgCAYAAAAffCjxAAAAAXNSR0IArs4c6QAAAFlJREFUSA3tksEKACAIQ7X//5zq98wOgQayum8QaGweHhMzG/6OujzKAymn+0LMqivu1XznWmX8/echTIyMyAgTwA72iIwwAexgj8gIE8CO3aMRbDPMaEy5BRGaKcZv8YxRAAAAAElFTkSuQmCC");
}

.uni-video-current-time,
.uni-video-duration {
  height: 14.5px;
  line-height: 14.5px;
  margin-top: 15px;
  margin-bottom: 14.5px;
  font-size: 12px;
  color: #cbcbcb;
}

.uni-video-progress-container {
  flex-grow: 2;
  flex-grow: 2;
  position: relative;
}

.uni-video-progress {
  height: 2px;
  margin: 21px 12px;
  background-color: rgba(255, 255, 255, 0.4);
  position: relative;
907
  cursor: pointer;
fxy060608's avatar
fxy060608 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
}

.uni-video-progress-buffered {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 100%;
  transition: width 0.1s;
  background-color: rgba(255, 255, 255, 0.8);
}

.uni-video-ball {
  width: 16px;
  height: 16px;
  padding: 14px;
  position: absolute;
  top: -21px;
  box-sizing: content-box;
  left: 0%;
  margin-left: -22px;
}

.uni-video-inner {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  border-radius: 50%;
}

.uni-video-danmu-button {
  white-space: nowrap;
  line-height: 1;
  padding: 2px 10px;
  border: 1px solid #fff;
  border-radius: 5px;
  font-size: 13px;
  color: #fff;
  margin: 0 8.5px;
947
  cursor: pointer;
fxy060608's avatar
fxy060608 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
}

.uni-video-danmu-button.uni-video-danmu-button-active {
  border-color: #48c23d;
  color: #48c23d;
}

.uni-video-fullscreen {
  width: 17px;
  height: 17px;
  padding: 8.5px;
  box-sizing: content-box;
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAiCAYAAAA6RwvCAAAAAXNSR0IArs4c6QAAAQRJREFUWAnt1d0NwiAQB/CmS7hHX5zFxLF0Ah2hE/lg7BT4PyMJUj6Oyt299BIioZT7ARYG59wLpTXmoXOMGO/QecxtwyWW4o42AupGALkFdX1MkHxE3Q7jIbQPqNthQogpJoZkMLRlsn/gFMQEk4OoY0oQVUwNoobhQFQwgMxUKFkt0C8+Zy61d8SeR5iHWCLOwF/MCb8Tp//ex3QFsE1HlCfKFUX2OijNFMnPKD7k76YcBoL402Zh8B77+MjlXrVvwfglXA32b0MrRgxCE2nBiEJaMOIQLkYFwsGoQWoYVUgJow4pYD4Weq4ayBqfwDYQmnUK0301kITujuawu65/l2B5A4z3Qe+Ut7EBAAAAAElFTkSuQmCC");
  background-size: 50%;
  background-position: 50% 50%;
  background-repeat: no-repeat;
964
  cursor: pointer;
fxy060608's avatar
fxy060608 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
}

.uni-video-fullscreen.uni-video-type-fullscreen {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAiCAYAAAA6RwvCAAABBElEQVRYhcXWwQ3CMAwF0O+qOzAKQzAAl0pMxQQwQhmGKwcERxbgc4lEVdHUbm3zJR8qJemTo6YByS3JO8kjyQbGkHQpkOz4zcmK8YQ0BWDCkOxL+UDKombMYKwfZAkmDGLFhEIsmHCIFpMC0WDSIHOYVEgNkw6pYPIhE5j/QCoYF0g7eEkPYGej+cX82x/l6aIAIOb9CcrajrjFE/IAQGP1IgIRcYVsVs32+vx+nC9nWq6dAZDhOaPHBEDGh54O4w0pa9oxEZBFmCjIBGb6Qh4JMWGiIWpMBkSFyYLMYjIhNUw7N9GQi2aQiLxJHspjV+rl1hFrRp25uV2MRGQRBsAewPUD/HhJVOOuCzwAAAAASUVORK5CYII=");
}

.uni-video-danmu {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  margin-top: 14px;
  margin-bottom: 44px;
  font-size: 14px;
  line-height: 14px;
  overflow: visible;
}

.uni-video-danmu-item {
  line-height: 1;
  position: absolute;
  color: #ffffff;
  white-space: nowrap;
  left: 100%;
  transform: translatex(0);
  transition-property: left, transform;
  transition-duration: 3s;
  transition-timing-function: linear;
}

.uni-video-toast {
  pointer-events: none;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.8);
  color: #000000;
  display: none;
}

.uni-video-toast.uni-video-toast-volume {
  width: 100px;
  height: 100px;
  display: block;
}

.uni-video-toast-volume .uni-video-toast-title {
  width: 100%;
  font-size: 12px;
  line-height: 16px;
  text-align: center;
  margin-top: 10px;
  display: block;
}

.uni-video-toast-volume .uni-video-toast-icon {
  fill: #000000;
  width: 50%;
  height: 50%;
  margin-left: 25%;
  display: block;
}

.uni-video-toast-volume .uni-video-toast-value {
  width: 80px;
  height: 5px;
  margin-top: 5px;
  margin-left: 10px;
}

.uni-video-toast-volume
  .uni-video-toast-value
  > .uni-video-toast-value-content {
  overflow: hidden;
}

.uni-video-toast-volume-grids {
  width: 80px;
  height: 5px;
}

.uni-video-toast-volume-grids-item {
  float: left;
  width: 7.1px;
  height: 5px;
  background-color: #000000;
}

.uni-video-toast-volume-grids-item:not(:first-child) {
  margin-left: 1px;
}

.uni-video-toast.uni-video-toast-progress {
  display: block;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  font-size: 14px;
  line-height: 18px;
  padding: 6px;
}
</style>