The installation

npm install vue-video-player --saveCopy the code

The introduction of

Plug-ins can be imported globally or individually within components that need them (either)

[1] Global reference, import and reference in main.js

import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
 
Vue.use(VideoPlayer)
Copy the code

[2] Component reference

import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'

export default {
  components: {
    videoPlayer
  }
}Copy the code

use

[1] HTML part

  <template>
    <div class='demo'>
      <video-player class="video-player vjs-custom-skin" 
            ref="videoPlayer" 
            :playsinline="true" 
            :options="playerOptions">
      </video-player>
    </div>
  </template>Copy the code

[2] JS part

    export default {
      data() {
        return {
          playerOptions: {
            playbackRates: [0.5.1.0.1.5.2.0].// Optional playback speed
            autoplay: false.// If true, playback starts when the browser is ready.
            muted: false.// Any audio will be eliminated by default.
            loop: false.// Whether to restart the video as soon as it ends.
            preload: 'auto'.// Suggest whether the browser should start downloading video data after the 
       
            language: 'zh-CN'.aspectRatio: '16:9'.// Place the player in smooth mode and use this value when calculating the player's dynamic size. The value should represent a scale - two numbers separated by colons (for example, "16:9" or "4:3")
            fluid: true.// When true, the Video.js player will have a fluid size. In other words, it will scale to fit its container.
            sources: [{
              type: "video/mp4"./ / type
              src: ' ' / / url}].poster: ' '.// Cover address
            notSupportedMessage: 'This video cannot play now, please try again later'.// Allows overwriting the default information displayed when video.js cannot play a media source.
            controlBar: {
              timeDivider: true.// The current time and duration delimiter
              durationDisplay: true.// Displays the duration
              remainingTimeDisplay: false.// Whether to display the remaining time function
              fullscreenToggle: true // Whether to display the full-screen button
            }
          }
        }
      }
    }Copy the code

[3] Effect drawing

Video format

Type:"video/webm"   // It can be played and opened with ogGType:"video/ogg"    // Can be played, webm can also be openedType:"video/3gp"    // It can be playedType:"video/mp4"    // It can be playedType:"video/avi"    / / can't open itType:"video/flv"    / / can't open itType:"video/mkv"    / / can't open itType:"video/mov"    / / can't open itType:"video/mpg"    / / can't open itType:"video/swf"    / / can't open itType:"video/ts"     / / can't open itType:"video/wmv"    / / can't open itType:"video/vob"    / / not conversionType:"video/mxf"    // Conversion error
type: "video/rm"     // Conversion errorCopy the code

Control play and pause

this.$refs.videoPlayer.player.play() / / play
this.$refs.videoPlayer.player.pause() / / pause
this.$refs.videoPlayer.player.src(src) // Reset the progress barCopy the code

The callback function

  <template>
    <div class='demo'>
      <video-player class="video-player vjs-custom-skin" 
        ref="videoPlayer" 
        :playsinline="true" 
        :options="playerOptions"
        @play="onPlayerPlay($event)" 
        @pause="onPlayerPause($event)"
        @ended="onPlayerEnded($event)"
        @waiting="onPlayerWaiting($event)"
        @playing="onPlayerPlaying($event)"
        @loadeddata="onPlayerLoadeddata($event)"
        @timeupdate="onPlayerTimeupdate($event)"
        @canplay="onPlayerCanplay($event)"
        @canplaythrough="onPlayerCanplaythrough($event)"
        @statechanged="playerStateChanged($event)"
        @ready="playerReadied"
      >
      </video-player>
    </div>
  </template>

  <script>
    export default {
      methods: {
        // Play the callback
        onPlayerPlay(player) {
          console.log('player play! ', player)
        },

        // Pause the callback
        onPlayerPause(player) {
          console.log('player pause! ', player)
        },

        // Video callback after playback
        onPlayerEnded($event) {
          console.log(player)
        },

        // readyState changes on DOM elements cause playback to stop
        onPlayerWaiting($event) {
          console.log(player)
        },

        // The callback has started playing
        onPlayerPlaying($event) {
          console.log(player)
        },

        // Triggered when the player downloads data at the current playing position
        onPlayerLoadeddata($event) {
          console.log(player)
        },

        // Triggered when the current playback position changes.
        onPlayerTimeupdate($event) {
          console.log(player)
        },

        // Media readyState is HAVE_FUTURE_DATA or higher
        onPlayerCanplay(player) {
          // console.log('player Canplay! ', player)
        },

        // Media readyState is HAVE_ENOUGH_DATA or higher. This means that the entire media file can be played without buffering.
        onPlayerCanplaythrough(player) {
          // console.log('player Canplaythrough! ', player)
        },

        // Play the state change callback
        playerStateChanged(playerCurrentState) {
          console.log('player current update state', playerCurrentState)
        },

        // Bind the listener to the ready state of the component. The difference with event listeners is that this function is fired immediately if the ready event has already occurred.
        playerReadied(player) {
          console.log('example player 1 readied', player); ,}}} </script>Copy the code



The article is updated every week. You can search “Front-end highlights” on wechat to read it in the first time, and reply to [Books] to get 200G video materials and 30 PDF books