How to use video.js in vue to play m3u8 format videos

How to use video.js in vue to play m3u8 format videos

@[toc] Note:

 "vue": "^2.6.11",
 "video.js": "^7.10.2",
 "videojs-contrib-hls": "^5.15.0",
 "mux.js": "^5.7.0"

1. Installation

yarn add video.js
yarn add videojs-contrib-hls // This is a plugin required to play hls streams yarn add mux.js // In the vue project, if it is not installed, an error may occur

2. Introducing videojs

1. Create a plugins folder under the src folder and create a video.js file;

The content of the video.js file is as follows:

import "video.js/dist/video-js.css"; // Import the CSS of video.js
import hls from "videojs-contrib-hls"; // Plug-in required to play hls stream import Vue from "vue";
Vue.use(hls);

2. Import the video.js file just now into main.js

import "./plugins/video.js"; // Import the video.js file just defined

3. Test and use in components

1. Implement basic automatic playback

Test.vue File

<template>
 <div class="test-videojs">
 <video id="videoPlayer" class="video-js" muted></video>
 </div>
</template>
<script>
import Videojs from "video.js"; // Import Videojs 
export default {
 data() {
 return {
 // https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8 is a video. You can replace cctv1 (which is a live broadcast and cannot be rewound) with it to see the effect of fast forward and rewind nowPlayVideoUrl: "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8"
 };
 },
 mounted() {
 this.initVideo(this.nowPlayVideoUrl);
 },
 methods: {
 initVideo(nowPlayVideoUrl) {
  // These options attributes can also be set directly on the video tag, see muted
  let options = {
  autoplay: true, // Set automatic playback controls: true, // Display playback controls sources: [
   // Note that if the src is set as option, channel switching cannot be achieved (even if nowPlayVideoUrl is monitored)
   {
   src: nowPlayVideoUrl,
   type: "application/x-mpegURL" // Tell videojs that this is an hls stream}
  ]
  };
  // The first parameter of videojs represents the id of the video in the document
  const myPlayer = Videojs("videoPlayer", options, function onPlayerReady() {
  console.log("this in onPlayerReady refers to:", this); // this here refers to Player, an instance created by Videojs console.log(myPlyer === this); // This returns true
  });
 }
 }
};
</script>
<style lang="scss">
#videoPlayer {
 width: 500px;
 height: 300px;
 margin: 50px auto;
}
</style>

The video playback effect is as shown below:

The print result is as follows:

2. Achieve channel switching

Test.vue File

<template>
 <div class="test-videojs">
 <video id="videoPlayer" class="video-js"></video>
 <el-button type="danger" @click="changeSource">Switch to CCTV3</el-button>
 </div>
</template>
<script>
import Videojs from "video.js"; // Import Videojs
export default {
 data() {
 return {
  nowPlayVideoUrl: "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8",
  options:
  autoplay: true, // Set autoplay muted: true, // Set it to true to achieve automatic playback, and the video will be muted (Chrome 66 and above prohibits automatic playback of audio and video)
  preload: "auto", // Preload controls: true // Display playback controls},
  player:null
 };
 },
 mounted() {
 this.getVideo(this.nowPlayVideoUrl);
 },
 methods: {
 getVideo(nowPlayVideoUrl) {
  this.player = Videojs("videoPlayer", this.options);
  //Key code, dynamically set src to implement channel switching operation this.player.src([
  {
   src: nowPlayVideoUrl,
   type: "application/x-mpegURL" // Tell videojs that this is an hls stream}
  ]);
 },
 changeSource() {
  this.nowPlayVideoUrl = "http://ivi.bupt.edu.cn/hls/cctv3hd.m3u8";
  console.log(this.nowPlayVideoUrl, "changed");
 }
 },
 watch:
 nowPlayVideoUrl(newVal, old) {
  this.getVideo(newVal);
 }
 },
 beforeDestroy() {
 if (this.player) {
  this.player.dispose(); // Removing Players, this method will reset the internal state of videojs and remove dom
 }
 }
};
</script>
<style lang="scss">
#videoPlayer {
 width: 500px;
 height: 300px;
 margin: 50px auto;
}
</style>

The video switching effect is as shown below:

4. Notes on pitfalls

1. The video cannot be played automatically or an error message is displayed: DOMException: play() failed

Need to use the muted attribute to solve

Error message: DOMException: play() failedbecause the user didn't interact with the document first. (The user has not interacted yet, so play cannot be called)

Solution: Set the muted property to true

 <video id="videoPlayer" class="video-js" muted></video>

About the muted attribute:

  • muted property, sets or returns whether the audio should be muted (turn off the sound); the property value is true and false;
  • muted="false" means that the video does not need to be muted (the video will play with sound), but if muted="false" is set, the video cannot be played automatically.
  • The function of muted in the video tag: Allow the video to play automatically; (Starting from Chrome version 66, automatic playback of video and audio is prohibited)

2. When changing channels, the URL has been successfully changed, but the video is still the same as before

You need to set src dynamically to achieve this

// Dynamically set src
this.player.src([
  {
   src: nowPlayVideoUrl,
   type: "application/x-mpegURL" // Tell videojs that this is an hls stream}
 ]);

3. Cannot find mux.js module

Error information: * mux.js/lib/tools/parse-sidx in ./node_modules/video.js/dist/video.es.js To install it, you can run: npm install --save mux.js/lib/tools/parse-sidx

Solution: Install mux.js

yarn add mux.js

5. Play rtmp stream

The operation of playing rtmp stream is almost the same as that of playing hls stream, except that:

import "videojs-flash"; // Plug-in required to play rtmp stream type: 'rtmp/flv', // This type value is required to tell videojs that this is an rtmp stream video

The above is the details of how to use video.js in vue to play videos in m3u8 format. For more information about vue using videojs to play videos in m3u8 format, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue uses the video tag to implement video playback
  • Vue-Video-Player operation for Vue
  • Detailed configuration of vue-video-player video player
  • vue-video-player implements real-time video playback (monitoring equipment-rtmp stream)
  • After the short video watermark is removed after parsing the Video tag playback in Vue, the video is unresponsive

<<:  MySQL 5.7.11 zip installation and configuration method graphic tutorial

>>:  How to use Nginx to realize the coexistence of multiple containers in the server

Recommend

Pull-down refresh and pull-up loading components based on Vue encapsulation

Based on Vue and native javascript encapsulation,...

The basic principles and detailed usage of viewport

1. Overview of viewport Mobile browsers usually r...

JavaScript css3 to implement simple video barrage function

This article attempts to write a demo to simulate...

JS, CSS and HTML to implement the registration page

A registration page template implemented with HTM...

Detailed explanation of CSS margin collapsing

Previous This is a classic old question. Since a ...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...

VMware Tools installation and configuration tutorial for Ubuntu

Some time ago, the blogger installed the Ubuntu s...

Solution to elementui's el-popover style modification not taking effect

When using element-ui, there is a commonly used c...

How to manage cached pages in Vue

Table of contents Problem 1: Destruction 1. How t...

mysql5.7.21.zip installation tutorial

The detailed installation process of mysql5.7.21 ...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...

Summary of Mysql update multi-table joint update method

Next, I will create two tables and execute a seri...