1

H5 comes with player

Advantage

  • Out of the box, support full screen and other functions

problem

The video can be played normally on the PC, but there are many problems when using the video tag on the mobile. You can refer to H5-Video can do things & existing pits

1. Video UI is inconsistent in different environments

image.png

2. Differences in API implementation and support in various environments:

See what the hell is this bunch of configurations below? ( References in this section )

<video
  src="video.mp4" 
  controls
  poster="images.jpg"
  preload="auto" 
  webkit-playsinline="true" 
  playsinline="true"
  x-webkit-airplay="allow" 
  x5-video-player-type="h5"
  x5-video-player-fullscreen="true"
  x5-video-orientation="portraint"
  style="object-fit:fill">
</video> 

different environments, we must do some configuration that meets its requirements, hoping to achieve the purpose we want as much as possible.

4. Media format support cannot be guaranteed

The overall support of MP4 is better, M3U8 is better in the high-version mobile environment, and there are a large number of flv playback requirements in the media-on-demand live broadcast. Native Video does not support it.

Handwriting mobile player

Target

accomplish

The first step: H5 adaptation template

    <video
      :src="videoSrc"
      :poster="defaultPoster"
      preload="auto"
      @click.stop.prevent="handleVideoClick"
      class="video__content"
      playsinline="true"
      x5-playsinline="true"
      webkit-playsinline="true"
      x-webkit-airplay="allow"
      x5-video-player-type="h5"
      x5-video-player-fullscreen="true"
      x5-video-orientation="portraint"
    ></video>

Step 2: poster

Principle: Put a div container on top of the video and let this container fill the video component. Load the picture by setting background-image

<template>
    <div
        class="video-poster"
        :style="style"
    >
    </div>
</template>
<script lang="ts">
import {
  Vue, Prop, Component
} from 'vue-property-decorator';

@Component({})
export default  class extends Vue {
  @Prop()
  public videoPoster?: string;

  get style() {
    return `background-image:url(${this.videoPoster})`;
  }
}
</script>
<style lang="scss" scoped>
.video-poster{
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 3; // 显示在video组件最上层
  background-repeat: no-repeat;
  background-size: 100% 100%; // 100%填充
  background-position: 50%;// 居中放置
}
</style>

Step 3: Realize the progress control of the scroll bar

Support click the progress bar to adjust the progress and drag the dot to adjust the progress: place a scroll bar container, and put an actual progress area and a dot element in it.

currentTime determines the progress of the progress

活动的进度条长度 / 进度条总长度 = currentTime / duration

By passing different currentTime progress can be achieved in different display .
On this basis, a full-screen progress bar and a non-full-screen progress bar containing actions such as play/pause can be encapsulated.

There are two options for progress bar advancement:

  1. The length of the activity progress bar is 100%. Initially, the activity progress bar is shifted to the left by 100%, and the setting beyond the container is hidden . Through the calculated distance, use translateX control the movement.

  2. Control the progress by the length of the activity progress bar 0-100%

The comparison found that flickering was acceptable when moving forward, and the second scheme was used.

  // active controls 样式
  public getWindowActiveMoveStyle(): Object {
    const percent = this.currentTime / this.duration;
    return {
      'width':`${Math.round(percent * 10000) / 100}%`,
      'background-color':`${this.activeControlBarColor}`
    };
  }
Click on the progress bar, converted to currentTime
  // 点击进度条,换算成currentTime
  private getTouchedTime(e: TouchEvent): number {
    const clientRects = this.windowProgressEl.getClientRects() || []; // IOS>=6
    const touchedClientX = e.touches[0].clientX;
    const touchedClientY = e.touches[0].clientY;
    const { left = 1, top = 0, width = 0, height= 0 } = clientRects[0] || {};

    /** 根据是否全屏,计算方式也有区别:主要是旋转之后一些属性值发生了变化 */
    const unit = this.fullscreen ? height : width;
    const touchedClientUnit = this.fullscreen ? touchedClientY : touchedClientX;
    const positionUnit = this.fullscreen ? top : left;
    return (touchedClientUnit - positionUnit) / unit * this.duration;
  }

Step 4: Realize full-screen playback

Need to pay attention to converting to currentTime after screen conversion

  private getTouchedTime(e: TouchEvent): number {
   ...

    /** 根据是否全屏,计算方式也有区别:主要是旋转之后一些属性值发生了变化 */
    const unit = this.fullscreen ? height : width;
    const touchedClientUnit = this.fullscreen ? touchedClientY : touchedClientX;
    const positionUnit = this.fullscreen ? top : left;
    return (touchedClientUnit - positionUnit) / unit * this.duration;
  }

other

Detail processing

Reference materials:


specialCoder
2.2k 声望168 粉丝

前端 设计 摄影 文学