index.vue 29.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
<template>
  <uni-video
    :id="id"
Q
qiang 已提交
4
    v-on="$listeners">
fxy060608's avatar
fxy060608 已提交
5 6 7 8
    <div
      ref="container"
      class="uni-video-container"
      @click="triggerControls"
Q
qiang 已提交
9 10 11 12 13
      @touchstart="touchstart"
      @touchend="touchend"
      @touchmove="touchmove"
      @fullscreenchange.stop="onFullscreenChange"
      @webkitfullscreenchange.stop="onFullscreenChange($event,true)"
14
    >
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19
      <video
        ref="video"
        :style="{opacity:!start?0.8:1,objectFit:objectFit}"
        :muted="muted"
        :loop="loop"
20
        :src="srcSync"
fxy060608's avatar
fxy060608 已提交
21
        :poster="poster"
Q
qiang 已提交
22
        :autoplay="autoplay"
Q
qiang 已提交
23
        v-bind="$attrs"
fxy060608's avatar
fxy060608 已提交
24 25 26
        class="uni-video-video"
        webkit-playsinline
        playsinline
Q
qiang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
        @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)"
40
      />
41 42 43
      <div
        v-show="controlsShow"
        class="uni-video-bar uni-video-bar-full"
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49
        @click.stop>
        <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"
50 51
            @click.stop="trigger"
          />
fxy060608's avatar
fxy060608 已提交
52 53 54 55
          <div class="uni-video-current-time">{{ currentTime|getTime }}</div>
          <div
            ref="progress"
            class="uni-video-progress-container"
56 57
            @click.stop="clickProgress($event)"
          >
fxy060608's avatar
fxy060608 已提交
58
            <div class="uni-video-progress">
59 60
              <div
                :style="{width:buffered*100+'%'}"
Q
qiang 已提交
61
                class="uni-video-progress-buffered" />
62 63 64
              <div
                ref="ball"
                :style="{left:progress+'%'}"
fxy060608's avatar
fxy060608 已提交
65
                class="uni-video-ball">
Q
qiang 已提交
66
                <div class="uni-video-inner" />
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75
              </div>
            </div>
          </div>
          <div class="uni-video-duration">{{ (duration||durationTime)|getTime }}</div>
        </div>
        <div
          v-if="danmuBtn"
          :class="{'uni-video-danmu-button-active':enableDanmuSync}"
          class="uni-video-danmu-button"
76 77
          @click.stop="triggerDanmu"
        >弹幕</div>
fxy060608's avatar
fxy060608 已提交
78 79 80 81
        <div
          v-show="showFullscreenBtn"
          :class="{'uni-video-type-fullscreen':fullscreen}"
          class="uni-video-fullscreen"
Q
qiang 已提交
82
          @click.stop="triggerFullscreen(!fullscreen)"
83
        />
fxy060608's avatar
fxy060608 已提交
84
      </div>
85 86 87 88
      <div
        v-show="start&&enableDanmuSync"
        ref="danmu"
        style="z-index: 0;"
Q
qiang 已提交
89
        class="uni-video-danmu" />
90 91 92
      <div
        v-if="!start"
        class="uni-video-cover"
fxy060608's avatar
fxy060608 已提交
93
        @click.stop>
94 95
        <div
          class="uni-video-cover-play-button"
Q
qiang 已提交
96
          @click.stop="play" />
fxy060608's avatar
fxy060608 已提交
97 98
        <p class="uni-video-cover-duration">{{ (duration||durationTime)|getTime }}</p>
      </div>
99 100
      <div
        :class="{'uni-video-toast-volume':gestureType==='volume'}"
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108
        class="uni-video-toast">
        <div class="uni-video-toast-title">音量</div>
        <svg
          class="uni-video-toast-icon"
          width="200px"
          height="200px"
          viewBox="0 0 1024 1024"
          version="1.1"
109 110 111 112 113
          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 已提交
114 115
        </svg>
        <div class="uni-video-toast-value">
116 117
          <div
            :style="{width:volumeNew*100+'%'}"
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122
            class="uni-video-toast-value-content">
            <div class="uni-video-toast-volume-grids">
              <div
                v-for="(item,index) in 10"
                :key="index"
123 124
                class="uni-video-toast-volume-grids-item"
              />
fxy060608's avatar
fxy060608 已提交
125 126 127 128
            </div>
          </div>
        </div>
      </div>
129 130
      <div
        :class="{'uni-video-toast-progress':gestureType=='progress'}"
fxy060608's avatar
fxy060608 已提交
131 132 133 134
        class="uni-video-toast">
        <div class="uni-video-toast-title">{{ currentTimeNew|getTime }} / {{ durationTime|getTime }}</div>
      </div>
    </div>
135 136 137
    <div
      style="position: absolute; top: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none;"
    >
Q
qiang 已提交
138
      <slot />
郭胜强 已提交
139
    </div>
fxy060608's avatar
fxy060608 已提交
140 141 142 143
  </uni-video>
</template>
<script>
import {
Q
qiang 已提交
144 145
  subscriber,
  interact
fxy060608's avatar
fxy060608 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
} 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: {
    getTime (time) {
Q
qiang 已提交
166 167 168
      let h = Math.floor(time / 3600)
      let m = Math.floor(time % 3600 / 60)
      let s = Math.floor(time % 3600 % 60)
fxy060608's avatar
fxy060608 已提交
169 170 171
      h = (h < 10 ? '0' : '') + h
      m = (m < 10 ? '0' : '') + m
      s = (s < 10 ? '0' : '') + s
Q
qiang 已提交
172
      let str = m + ':' + s
fxy060608's avatar
fxy060608 已提交
173 174 175 176 177 178
      if (h !== '00') {
        str = h + ':' + str
      }
      return str
    }
  },
Q
qiang 已提交
179
  mixins: [subscriber, interact],
fxy060608's avatar
fxy060608 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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
  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 已提交
233
      default: ''
fxy060608's avatar
fxy060608 已提交
234
    },
235 236 237 238
    showProgress: {
      type: Boolean,
      default: true
    },
fxy060608's avatar
fxy060608 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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
    }
  },
  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 已提交
282
      isSafari: /^Apple/.test(navigator.vendor)
fxy060608's avatar
fxy060608 已提交
283 284 285 286 287 288 289 290
    }
  },
  computed: {
    controlsShow () {
      return this.start && this.controls && this.controlsVisible
    },
    autoHideContorls () {
      return this.controlsShow && this.playing && !this.controlsTouching
291 292 293
    },
    srcSync () {
      return this.$getRealPath(this.src)
fxy060608's avatar
fxy060608 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
    }
  },
  watch: {
    enableDanmuSync (val) {
      this.$emit('update:enableDanmu', val)
    },
    autoHideContorls (val) {
      if (val) {
        this.autoHideStart()
      } else {
        this.autoHideEnd()
      }
    },
307
    srcSync (val) {
fxy060608's avatar
fxy060608 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
      this.playing = false
      this.currentTime = 0
    },
    currentTime () {
      this.updateProgress()
    },
    duration () {
      this.updateProgress()
    }
  },
  created () {
    this.otherData = {
      danmuList: [],
      danmuIndex: {
        time: 0,
        index: -1
      },
      hideTiming: null
    }
Q
qiang 已提交
327
    const danmuList = this.otherData.danmuList = JSON.parse(JSON.stringify(this.danmuList || []))
fxy060608's avatar
fxy060608 已提交
328 329 330 331 332
    danmuList.sort(function (a, b) {
      return (a.time || 0) - (a.time || 0)
    })
  },
  mounted () {
Q
qiang 已提交
333 334 335 336 337 338 339 340 341
    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 已提交
342 343
      originX = toucher.pageX
      originY = toucher.pageY
Q
qiang 已提交
344
      originProgress = this.progress
fxy060608's avatar
fxy060608 已提交
345
      moveOnce = true
Q
qiang 已提交
346
      this.touching = true
fxy060608's avatar
fxy060608 已提交
347 348 349 350
      ball.addEventListener('touchmove', touchmove, passiveOptions)
    })

    function touchmove (event) {
Q
qiang 已提交
351 352 353
      const toucher = event.targetTouches[0]
      const pageX = toucher.pageX
      const pageY = toucher.pageY
fxy060608's avatar
fxy060608 已提交
354 355 356 357 358
      if (moveOnce && Math.abs(pageX - originX) < Math.abs(pageY - originY)) {
        touchend()
        return
      }
      moveOnce = false
Q
qiang 已提交
359 360
      const w = self.$refs.progress.offsetWidth
      let progress = originProgress + (pageX - originX) / w * 100
fxy060608's avatar
fxy060608 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
      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 已提交
387
    this.triggerFullscreen(false)
fxy060608's avatar
fxy060608 已提交
388 389 390 391 392 393 394
    clearTimeout(this.otherData.hideTiming)
  },
  methods: {
    _handleSubscribe ({
      type,
      data = {}
    }) {
Q
qiang 已提交
395 396
      const methods = ['play', 'pause', 'seek', 'sendDanmu', 'playbackRate', 'requestFullScreen', 'exitFullScreen']
      let options
fxy060608's avatar
fxy060608 已提交
397 398
      switch (type) {
        case 'seek':
Q
qiang 已提交
399
          options = data.position
fxy060608's avatar
fxy060608 已提交
400 401
          break
        case 'sendDanmu':
Q
qiang 已提交
402
          options = data
fxy060608's avatar
fxy060608 已提交
403 404
          break
        case 'playbackRate':
Q
qiang 已提交
405
          options = data.rate
fxy060608's avatar
fxy060608 已提交
406 407
          break
      }
Q
qiang 已提交
408 409
      if (methods.indexOf(type) >= 0) {
        this[type](options)
fxy060608's avatar
fxy060608 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      }
    },
    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 已提交
433 434 435 436
      const $progress = this.$refs.progress
      let element = event.target
      let x = event.offsetX
      while (element !== $progress) {
fxy060608's avatar
fxy060608 已提交
437 438 439
        x += element.offsetLeft
        element = element.parentNode
      }
Q
qiang 已提交
440 441
      const w = $progress.offsetWidth
      let progress = 0
fxy060608's avatar
fxy060608 已提交
442 443 444 445 446 447 448 449 450
      if (x >= 0 && x <= w) {
        progress = x / w
        this.seek(this.$refs.video.duration * progress)
      }
    },
    triggerDanmu () {
      this.enableDanmuSync = !this.enableDanmuSync
    },
    playDanmu (danmu) {
Q
qiang 已提交
451
      const p = document.createElement('p')
fxy060608's avatar
fxy060608 已提交
452 453
      p.className = 'uni-video-danmu-item'
      p.innerText = danmu.text
Q
qiang 已提交
454
      let style = `bottom: ${Math.random() * 100}%;color: ${danmu.color};`
fxy060608's avatar
fxy060608 已提交
455 456 457 458 459 460 461 462 463 464 465
      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 已提交
466
      const otherData = this.otherData
fxy060608's avatar
fxy060608 已提交
467 468 469 470 471 472
      otherData.danmuList.splice(otherData.danmuIndex.index + 1, 0, {
        text: String(danmu.text),
        color: danmu.color,
        time: this.$refs.video.currentTime || 0
      })
    },
Q
qiang 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    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) {
          document[document.fullscreenEnabled ? 'exitFullscreen' : 'webkitExitFullscreen']()
        } 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 已提交
506
    },
Q
qiang 已提交
507 508 509
    onFullscreenChange ($event, webkit) {
      if (webkit && document.fullscreenEnabled) {
        return
fxy060608's avatar
fxy060608 已提交
510
      }
Q
qiang 已提交
511 512 513 514 515 516 517 518
      this.emitFullscreenChange(!!(document.fullscreenElement || document.webkitFullscreenElement))
    },
    emitFullscreenChange (val) {
      this.fullscreen = val
      this.$trigger('fullscreenchange', {}, {
        fullScreen: val,
        direction: 'vertical'
      })
fxy060608's avatar
fxy060608 已提交
519
    },
Q
qiang 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    requestFullScreen () {
      this.triggerFullscreen(true)
    },
    exitFullScreen () {
      this.triggerFullscreen(false)
    },
    onDurationChange ({ target }) {
      this.durationTime = target.duration
    },
    onLoadedMetadata ({ target }) {
      const initialTime = Number(this.initialTime) || 0
      if (initialTime > 0) {
        target.currentTime = initialTime
      }
    },
    onProgress ({ target }) {
      const buffered = target.buffered
      if (buffered.length) {
        this.buffered = buffered.end(buffered.length - 1) / target.duration
      }
    },
    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++) {
          let element = danmuList[index]
          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--) {
          let element = danmuList[index]
          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 已提交
598 599 600 601 602
    },
    triggerControls () {
      this.controlsVisible = !this.controlsVisible
    },
    touchstart (event) {
Q
qiang 已提交
603
      const toucher = event.targetTouches[0]
fxy060608's avatar
fxy060608 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
      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 已提交
620
      const gestureType = this.gestureType
fxy060608's avatar
fxy060608 已提交
621 622 623
      if (gestureType === GestureType.STOP) {
        return
      }
Q
qiang 已提交
624 625 626 627
      const toucher = event.targetTouches[0]
      const pageX = toucher.pageX
      const pageY = toucher.pageY
      const origin = this.touchStartOrigin
fxy060608's avatar
fxy060608 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
      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()
        }
646
      } else {
fxy060608's avatar
fxy060608 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
        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 已提交
669 670
      const duration = this.$refs.video.duration
      let currentTimeNew = x / 600 * duration + this.currentTimeOld
fxy060608's avatar
fxy060608 已提交
671 672 673 674 675 676 677 678
      if (currentTimeNew < 0) {
        currentTimeNew = 0
      } else if (currentTimeNew > duration) {
        currentTimeNew = duration
      }
      this.currentTimeNew = currentTimeNew
    },
    changeVolume (y) {
Q
qiang 已提交
679 680
      const valueOld = this.volumeOld
      let value
fxy060608's avatar
fxy060608 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
      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 已提交
698
      const otherData = this.otherData
fxy060608's avatar
fxy060608 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
      if (otherData.hideTiming) {
        clearTimeout(otherData.hideTiming)
        otherData.hideTiming = null
      }
    },
    updateProgress () {
      if (!this.touching) {
        this.progress = this.currentTime / this.durationTime * 100
      }
    }
  }
}
</script>

<style>
714
uni-video {
fxy060608's avatar
fxy060608 已提交
715 716 717 718 719 720 721 722
  width: 300px;
  height: 225px;
  display: inline-block;
  line-height: 0;
  overflow: hidden;
  position: relative;
}

723
uni-video[hidden] {
724
  display: none;
725 726
}

fxy060608's avatar
fxy060608 已提交
727 728 729 730 731 732 733 734 735
.uni-video-container {
  width: 100%;
  height: 100%;
  background-color: black;
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
736
  object-position: inherit;
fxy060608's avatar
fxy060608 已提交
737 738 739 740 741 742 743 744 745 746
}

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

.uni-video-video {
  width: 100%;
  height: 100%;
747
  object-position: inherit;
fxy060608's avatar
fxy060608 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
}

.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%;
}

.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;
792 793
  /* 解决全屏后被 video 遮挡的问题 */
  transform: translate3d(0, 0, 0);
fxy060608's avatar
fxy060608 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 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 907 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 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 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
}

.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;
}

.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;
}

.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;
}

.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;
}

.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>