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

How to use mysql to complete the data generation in excel

Excel is the most commonly used tool for data ana...

mysql indexof function usage instructions

As shown below: LOCATE(substr,str) Returns the fi...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...

Detailed explanation of the Docker deployment tutorial for Jenkins beginners

This article deploys Jenkins+Maven+SVN+Tomcat thr...

Difference between HTML ReadOnly and Enabled

The TextBox with the ReadOnly attribute will be di...

What is MIME TYPE? MIME-Types type collection

What is MIME TYPE? 1. First, we need to understand...

MySQL scheduled task example tutorial

Preface Since MySQL 5.1.6, a very unique feature ...

Detailed explanation of mysql integrity constraints example

This article describes the MySQL integrity constr...

Detailed installation history of Ubuntu 20.04 LTS

This article records the creation of a USB boot d...

Summary of the three stages of visual designer growth

Many people have read this book: "Grow as a ...

How to use JSX in Vue

What is JSX JSX is a syntax extension of Javascri...

Detailed explanation of the usage of image tags in HTML

In HTML, the <img> tag is used to define an...

JavaScript array reduce() method syntax and example analysis

Preface The reduce() method receives a function a...

Detailed explanation of CSS elastic box flex-grow, flex-shrink, flex-basis

The functions of the three attributes flex-grow, ...