Implementation example of video player based on Vue

Implementation example of video player based on Vue

When the existing video player cannot meet the needs, you need to encapsulate the video yourself.

Video Events

  • loadstart: Triggered when the video starts loading, assigning a value to currentTime (historical playback record or 0).
  • durationchange: Meta information has been loaded or changed, and the length of the video has changed. Get the video length (duration, and assign a value to currentTime (historical playback record or 0)).
  • playing: Fired when the video starts playing (either for the first time, resuming after being paused, or restarting after it has ended).
  • pause: Triggered when playback is paused.
  • timeupdate: currentTime changes, update the playback progress. Processing playback progress bar
  • seeked: specifies the playback position
  • waiting: buffer
  • ended: playback ended, reset status
  • WeixinJSBridgeReady: To use video in WeChat, you need to listen to the weixinJSBridgeReady event and execute the play() command in the callback function.

Live Broadcast Protocol

HLS (HTTP Live Streaming) is a live streaming protocol proposed by Apple. Both iOS and higher versions of Android support HLS. HLS mainly consists of two playback files: .m3u8 and .ts. HLS has high compatibility and scalability, but it will cause live broadcast delay. The HLS protocol divides the live stream into small segments for download and playback. So assuming that the list contains 5 ts files and each ts file contains 5 seconds of video content, the overall delay is 25 seconds.

RTMP (Real Time Messaging Protocol) is a set of live video protocols developed by Macromedia, which now belongs to Adobe. RTMP is based on flash and cannot be played in iOS browsers, but its real-time performance is better than HLS.

HTTP-FLV is a live distribution stream for FLV video format with low latency. But it is not supported on mobile devices.

Conclusion: HTTP-FLV has low latency and is preferred. If the device does not support HTTP-FLV, use flv.js. If the device does not support flv.js, HLS is used, but HLS has a large delay.

Encapsulating video

/** HTML **/
<div class="video">
 <!-- video player -->
 <video
  class="video__player"
  ref="videoPlayer"
  preload="auto"
  :autoplay="options.autoplay"
  :muted="options.muted"
  webkit-playsinline="true"
  playsinline="true"
  x-webkit-airplay="allow"
  x5-video-player-type="h5-page"
  x5-video-orientation="portraint"
  style="object-fit:cover;"
 >
  <source :src="options.src" />
 </video>

 <!-- video poster -->
 <div
  v-show="showPoster"
  class="video__poster"
  :style="{'background-image': 'url(' + options.pic + ')'}"
 ></div>

 <!-- video loading -->
 <div v-show="showLoading" class="video__Loading">
  <span class="video__Loading-icon"></span>
 </div>

 <!-- video pause -->
 <div @click="handleVideoPlay" class="video__pause">
  <span v-show="!videoPlay" class="video__pause-icon"></span>
 </div>
</div>
/**js**/
props: {
 options:
  type: Object,
  default: () => {}
 }
},
data() {
 return {
  videoPlay: false, // Is the video currently playing videoEnd: false, // Has the video ended? showPoster: true, // Is the video cover displayed? showLoading: false, // Loading currentTime: 0, // Current playback position currentVideo: {
   duration: this.options.duration
  },

 }
},
mounted() {
 this.videoPlayer = this.$refs.videoPlayer;
 this.videoPlayer.currentTime = 0;
 
 this.videoPlayer.addEventListener("loadstart", e => {
   this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
 });
 
  // Get the video length this.videoPlayer.addEventListener("durationchange", e => {
  this.currentVideo.duration = this.videoPlayer.duration;
  // Playback history exists this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
 });
 
 this.videoPlayer.addEventListener("playing", e => {
  this.showPoster = false;
  this.videoPlay = true;
  this.showLoading = false;
  this.videoEnd = false;
 });
 
 // Pause this.videoPlayer.addEventListener("pause", () => {
  this.videoPlay = false;
  if (this.videoPlayer.currentTime < 0.1) {
   this.showPoster = true;
  }
  this.showLoading = false;
 });
 
 // Playback progress update this.videoPlayer.addEventListener("timeupdate", e => {
   this.currentTime = this.videoPlayer.currentTime;
  },
  false
 );

 // Specify the playback position this.videoPlayer.addEventListener("seeked", e => {
 });

 // Buffering this.videoPlayer.addEventListener("waiting", e => {
  this.showLoading = true;
 });
 
 // Playback ends this.videoPlayer.addEventListener("ended", e => {
  // Reset state this.videoPlay = false;
  this.showPoster = true;
  this.videoEnd = true;
  this.currentTime = this.currentVideo.duration;
 });
 
 // Listen for the weixinJSBridgeReady event to handle the problem of WeChat not being able to play automatically. But it is not applicable to all of them. A pause button is added for manual playback.
 document.addEventListener('WeixinJSBridgeReady', () => { 
  this.videoPlayer.play();
 }, false);
},
methods: {
 handleVideoPlay() {
  if (this.videoPlayer.paused) { // Play if (this.videoEnd) { // Replay this.currentTime = 0;
    this.videoPlayer.currentTime = 0;
   }
   this.videoPlayer.play();
   this.videoPlay = true;
  } else { // Pause this.videoPlayer.pause();
   this.videoPlay = false;
  }
 }
}

[Reference Article]:

  • X5 core video two playback modes
  • H5 Live Video Label Pitfalls Summary
  • Introduction to H5 Live Broadcasting (Theory)
  • Comprehensive advanced H5 live broadcast

This is the end of this article about the implementation example of the vue-based video player. For more relevant vue video player content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue-Video-Player operation for Vue
  • Detailed configuration of vue-video-player video player
  • Method of customizing player based on vue-video-player

<<:  Detailed explanation of configuring keepalived log to another path in centos7

>>:  Instances of excluding certain libraries when backing up the database with mysqldump

Recommend

How to deploy python crawler scripts on Linux and set up scheduled tasks

Last year, due to project needs, I wrote a crawle...

MySQL constraint types and examples

constraint Constraints ensure data integrity and ...

Perform data statistics on different values ​​of the same field in SQL

Application scenario: It is necessary to count th...

Docker installs the official Redis image and enables password authentication

Reference: Docker official redis documentation 1....

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...

vue-cli introduction and installation

Table of contents 1. Introduction 2. Introduction...

Use Docker to build a Git image using the clone repository

Overview I have been using Docker for more than a...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

Summarize the User-Agent of popular browsers

1. Basic knowledge: Http Header User-Agent User A...

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

How to create your own Docker image and upload it to Dockerhub

1. First register your own dockerhub account, reg...