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

HTML web page creation tutorial Use iframe tags carefully

Using iframes can easily call pages from other we...

Detailed explanation of the steps to build a Vue project with Vue-cli

First you need to install Vue-cli: npm install -g...

Attributes and usage of ins and del tags

ins and del were introduced in HTML 4.0 to help au...

Introduction to /etc/my.cnf parameters in MySQL 5.7

Below are some common parameters of /etc/my.cnf o...

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

In-depth understanding of this in JavaScript

In-depth understanding of this in Js JavaScript s...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

Interviewer asked how to achieve a fixed aspect ratio in CSS

You may not have had any relevant needs for this ...

Complete steps to set up automatic updates in CentOS 8

The best thing you can do for your data and compu...

Pure CSS to achieve the water drop animation button in Material Design

Preface You should often see this kind of special...

Example code for implementing a text marquee with CSS3

Background Here's what happened, Luzhu accide...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...

Nginx rewrite regular matching rewriting method example

Nginx's rewrite function supports regular mat...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...