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

How to import Excel files into MySQL database

This article shares with you how to import Excel ...

Detailed examples of the difference between methods watch and computed in Vue.js

Table of contents Preface introduce 1. Mechanism ...

Detailed explanation of the pitfalls of nginx proxy socket.io service

Table of contents Nginx proxies two socket.io ser...

Research on the problem of flip navigation with tilted mouse

In this article, we will analyze the production of...

Explore how an LED can get you started with the Linux kernel

Table of contents Preface LED Trigger Start explo...

In-depth explanation of special permissions SUID, SGID and SBIT in Linux

Preface For the permissions of files or directori...

JS 4 super practical tips to improve development efficiency

Table of contents 1. Short circuit judgment 2. Op...

Simple implementation of vue drag and drop

This article mainly introduces the simple impleme...

MySQL isolation level detailed explanation and examples

Table of contents 4 isolation levels of MySQL Cre...

Comprehensive analysis of MySql master-slave replication mechanism

Table of contents Master-slave replication mechan...

Nginx URL rewriting mechanism principle and usage examples

URL rewriting helps determine the preferred domai...

Typical cases of MySQL index failure

Table of contents Typical Cases Appendix: Common ...