How to use vue-video-player to achieve live broadcast

How to use vue-video-player to achieve live broadcast

Preparation before class: Live streaming protocol https://www.cnblogs.com/yangchin9/p/14930874.html

Abstract: Watch live broadcast + video playback on H5 page; During the development, I used plugins such as video.js and mui-player, but found that these video plugins are not friendly to mobile compatibility. Finally, I found a plugin with good compatibility on mobile terminals - vue-video-player

Scenario:

Mobile H5 page, the project is built with vue, using the HLS protocol and ending with m3u8 live stream

1. Install vue-video-player

To play HLS video streams, you need to install the videojs-contrib-hls plug-in. To play RTMP video streams, you need to install the videojs-flash plug-in. When the hls plug-in and the flash plug-in are used at the same time, the flash plug-in needs to be introduced before the hls plug-in; (the document says that the hls plug-in will be automatically installed when installing vue-video-player, but it is not in actual operation, so you should install it manually!)

Installation method 1:

CDN method, directly introduce the file in the HTML file header:
  <link rel="stylesheet" href="path/to/video.js/dist/video-js.css"/>
  <script type="text/javascript" src="path/to/video.min.js"></script>
  <script type="text/javascript" src="path/to/vue.min.js"></script>
  <script type="text/javascript" src="path/to/dist/vue-video-player.js">
  </script>
    <script type="text/javascript"> Vue.use(window.VueVideoPlayer)
  </script>

Installation method 2:

Click to view the code

NMP installation plugin:
  npm install vue-video-player --save
    npm install videojs-contrib-hls --save

Introduce the basic style file in main.js:
// Import videoPlayer style import 'video.js/dist/video-js.css'
    

Import on demand:
import 'videojs-contrib-hls'
import { videoPlayer } from 'vue-video-player'

components:
    videoPlayer
},    
 
Global import:
import Vue from 'vue'
import VueVideoPlayer from 'vue-video-player' 
// Import custom styles and add the corresponding class name in <video-player>, such as video-player-custom
// import 'vue-video-player/src/custom-theme.css'
Vue.use(VueVideoPlayer, /* { 
 options: global default configuration,
   events: global videojs events}*/)

2. Use vue-video-player

<template>
  <!-- playsinline: Set the player to not be full screen on mobile devices [ Boolean, default: false ] -->
  <!-- customEventName: custom state change event name [String, default: 'statechanged'] -->
    <video-player
    class="video-player-custom"
        ref="videoPlayer"
        :options="playerOptions"
        :playsinline="true"
        customEventName="customstatechangedeventname"
        @play="onPlayerPlay"
        @pause="onPlayerPause"
        @ended="onPlayerEnded"
        @ready="playerReadied"
        @statechanged="playerStateChanged"
        @playing="onPlayerPlaying"
        @waiting="onPlayerWaiting"
        @loadeddata="onPlayerLoadeddata"
        @timeupdate="timeupdate"
        @canplay="onPlayerCanplay"
        @canplaythrough="onPlayerCanplaythrough">
    </video-player>
</template>
<script>
import 'videojs-contrib-hls'
import { videoPlayer } from 'vue-video-player'
export default {
   components:
        videoPlayer
    },
  data() { 
    return {
       playerOptions: { 
         // Is it muted: true,
                  // The default is English, set to Chinese language: 'zh-CN',
                  // Playback speed. After specifying it, Video.js will display a control (a control of the vjs-playback-rate class) to allow the user to select the playback speed playbackRates: [0.5, 1.0, 1.5, 2.0],
                  // Put the player into smooth mode and use this value when calculating the dynamic size of the player, indicating the aspect ratio aspectRatio: '4:3',
             // Compatibility order, the default value is ['html5'], other registered technologies will be added after this technology in the order in which they were registered.
                  techOrder: ['html5'],
                  // Equivalent to a set of <source> subtags in the native <video> tag, which can achieve graceful degradation; the type attribute specifies the MIME type of the media resource sources: [
                        //{
                    	  //type: "video/mp4",
                    	  //src: ""
                  	//},
                        //{
                    	  //type: "rtmp/flv",
                    	  //src: ""
                  	//},
                   	{
                     	  type: "application/x-mpegURL",
                     	  src: "https://live.cgtn.com/1000/prog_index.m3u8"
                   	},
                   ],
                  // Video cover poster: require('./icon/cover.jpg'), 
      }
    }
  },
  computed: { 
    	//Plugin node is used to add custom button events player() { 
      return this.$refs.videoPlayer.player 
    }
  },
    mounted() {},
    methods: {
        // Play callback onPlayerPlay(player) {
        },
        // Pause callback onPlayerPause(player) {
          //console.log(player)
        },
        //Video playback ends callback onPlayerEnded(player) {
          //console.log(player)
        },
        // readyState changes on DOM element cause playback to stop onPlayerWaiting(player) {
          //console.log(player)
        },
        // Playback has started callback onPlayerPlaying(player) {
          //console.log(player)
        },
        // When the player downloads data at the current playback position, onPlayerLoadeddata($event) is triggered {
          //console.log($event)
        },
        // Triggered when the current playback position changes.
        onPlayerTimeupdate($event) {
          //console.log($event)
        },
        //The media's readyState is HAVE_FUTURE_DATA or higher onPlayerCanplay($event) {
          //console.log($event)
        },
        // The media's readyState is HAVE_ENOUGH_DATA or higher. This means that the entire media file can be played without buffering.
        onPlayerCanplaythrough($event) {
          //console.log($event)
        },
        //Playback status change callback playerStateChanged($event) {
          //console.log($event)
        },
        //Bind a listener to the component's ready state. The difference with event listeners is that if the ready event has already occurred, it will trigger the function immediately. .
        playerReadied($event) {
          //console.log($event)
        },
    }
}
</script>
<style lang="scss" scoped>
.video-player-custom{
    width: 100%;
    height: 180px;
    // vidoeUI rewrites /deep/ .video-js{
        width: 100%;
        height: 100%;
        padding: 0;
        overflow: hidden;
        // player.vjs-tech{
          object-fit: cover;
        }
        // Play button.vjs-big-play-button {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 2em;
            height: 2em;
            margin-top: -1em;
            margin-left: -1em;
            font-size: 2em;
            line-height: 2em;
            border-radius: 50%;
            background-color: rgba(0,0,0,0.45);
            outline: none;
        }
        // Cover.vjs-poster{
            width: 100%;
            height: 100%;
            background-size: cover;
        }
    }
}  
</style>

This is the end of this article about using vue-video-player to achieve live broadcast. For more relevant vue-video-player live broadcast content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of breakpoint resume in vue-video-player
  • Tutorial on how to use vue video and vue-video-player to spread videos
  • Detailed explanation of using vue-video-player as a video plugin in vue
  • Detailed configuration of vue-video-player video player
  • Example of using the video plugin vue-video-player in vue
  • How to use the vue-video-player plugin for vue video playback
  • Detailed explanation of vue-video-player usage experience (compatible with m3u8)
  • Implementation of vue-video-player playing m3u8 video stream

<<:  Neon light effects implemented with pure CSS3

>>:  HTML n ways to achieve alternate color code sample code

Recommend

Solution to the failure of loading dynamic library when Linux program is running

Unable to load dynamic library under Linux When t...

MySQL 8.0.18 stable version released! Hash Join is here as expected

MySQL 8.0.18 stable version (GA) was officially r...

A brief discussion on the types of node.js middleware

Table of contents Overview 1. Application-level m...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

Analysis of Facebook's Information Architecture

<br />Original: http://uicom.net/blog/?p=762...

Understanding the Lazy Loading Attribute Pattern in JavaScript

Traditionally, developers create properties in Ja...

How to lock a virtual console session on Linux

When you are working on a shared system, you prob...

Detailed tutorial on installing MySQL database in Linux environment

1. Install the database 1) yum -y install mysql-s...

Detailed explanation of how to view the current number of MySQL connections

1. View the detailed information of all current c...

Pure CSS to achieve cloudy weather icon effect

Effect The effect is as follows ​ Implementation ...

How to use Xtrabackup to back up and restore MySQL

Table of contents 1. Backup 1.1 Fully prepared 1....

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

How to use echarts to visualize components in Vue

echarts component official website address: https...

Implementation of setting fixed IP when starting docker container

Network type after docker installation [root@insu...