Home  >  Article  >  Web Front-end  >  How to add video in vuejs

How to add video in vuejs

藏色散人
藏色散人Original
2021-11-01 14:47:205563browse

How to add video in vuejs: 1. Insert the video link through iframe; 2. Add the video by citing the vue-video-player plug-in.

How to add video in vuejs

The operating environment of this article: windows7 system, vue2.9.6 version, DELL G3 computer.

How to add video to vuejs?

Summary of 2 methods to insert video based on Vue:


Method 1: Insert video link into iframe

1.1

## Currently playing video

<div class="video-wrap" style="width:80%;float:left;oveflow:hidden;">
     <iframe :src="this.activeVideo.youtobeURL" frameborder=&#39;0&#39;
     allow=&#39;autoplay;encrypted-media&#39; allowfullscreen style=&#39;width:100%;height:500px;&#39;>
     </iframe>
     <h3>{{this.activeVideo.title}}</h3>
    </div>

1.2

##Video list

<div class="video-list" style="float:right;width:20%;text-align:center;">
    <div v-for=&#39;video in videos&#39; :key=&#39;video.id&#39; class="thumbnail" >
     <div class="thumbnail-img" >
      <div style="height:50%;width:100%;position:absolute;z-index:999"
      @click="activeVideoShow(video.id)"></div>
     <iframe :src=&#39;video.youtobeURL&#39; :alt="video.title" />
     </div>
     <div class="thumbnail-info">
      <h4>{{video.title}}</h4>
      <div class="thumbnail-views">
      <span>{{video.speaker}}</span>
      <span>{{video.views}} Views</span>
      </div>
      <div class="thumbnail-describe">
      {{video.describe}}
      </div>
     </div>
    </div>
   </div>

1.3

## Defined data structure (demo written by myself, may be practical The data structure returned in the middle and backend will be different)

data () {
  return {
   flag:false,
   videos:[{
    id:1,title:&#39;test2&#39;,youtobeURL:&#39;http://player.youku.com/embed/XMzcwNTY3NTM2MA&#39;,speaker:&#39;harry&#39;, likes:101,views:0,describe:&#39;good&#39;
   },{
    id:2,title:&#39;test3&#39;,youtobeURL:&#39;http://player.youku.com/embed/XMzcwNTY3NTM2MA&#39;,speaker:&#39;harry&#39;, likes:100,views:75,describe:&#39;good&#39;
   }],
   activeVideo:{
    id:3,title:&#39;test1&#39;,thumbnail:&#39;./../../static/images/headImg.png&#39;,speaker:&#39;harry&#39;, likes:0,views:0,describe:&#39;good&#39;,
    youtobeURL:&#39;http://player.youku.com/embed/XMzcwNTY3NTM2MA&#39;
   }
  }
 }

1.4

## Click the video in the video list to change to the current video

ps: At the beginning, the click event was written on the iframe, but the click was invalid. Later I wrote a div, the perfect solution:

   <div style="height:50%;width:100%;position:absolute;z-index:999"
      @click="activeVideoShow(video.id)"></div>

1.5

##Convert the click event of the current video: Use the id to determine which one is currently clicked

activeVideoShow(id){
  this.videos.filter(item=>{
     if(id == item.id){
      this.activeVideo=item
     }
    })
  }

Method 2: Reference the vue-video-player plug-in (no video list)

Write a bunch of divs relative to the iframe method And style, vue-video-player is simply streamlined to take off

2.1

##The first time I used this plug-in, I was not very familiar with it, so I wrote a videoPlayer component based on the official API. The code is as follows:

<div>
    <video ref="videoPlayer" class="video-js"></video>
  </div>

2.1-1

##Need to introduce video.js and define related options

import videojs from &#39;video.js&#39;
---------------------------------
props:{
    options:{
      type:Object,
      default(){
        return{
        }
      }
    }
  },
data(){
    return{
      player:null
    }
  },
mounted(){
    this.player=videojs(this.$refs.videoPlayer,this.options,function onPlayerReady(){
      console.log(&#39;onPlayerReady&#39;,this)
    })
  }

2.2

##Introduce the above videoPlayer component into the page where the video is inserted, and the code in the view layer is as follows:

<video-player class="video-player vjs-custom-skin "
    ref="videoPlayer"
    :playsinline=&#39;false&#39;
    :options=&#39;videoOptions&#39;
    @play="onPlayerPlay($event)"
    @pause=&#39;onPlayerPause($event)&#39;
    @statechagned=&#39;playerStateChanged($event)&#39;
    >
    </video-player>


2.3

##Plug-ins that need to be introduced

import &#39;./../../node_modules/video.js/dist/video-js.css&#39;
import &#39;./../../node_modules/vue-video-player/src/custom-theme.css&#39;
import videojs from &#39;video.js&#39;
import {videoPlayer} from &#39;vue-video-player&#39;
import &#39;videojs-flash&#39;
import VideoPlayer from &#39;@/components/videoPlayer.vue&#39;

2.3-1

##Define related data

props:{
   state:Boolean,
  },
data(){
    return{
      videoOptions:{
        playbackRates:[1.0,1.5,2.0], // 播放速度
        autoplay:false, // 如果true,浏览器准备好时开始回放
        controls:true,
        muted:false, // 默认情况下将会消除任何音频
        loop:false, //循环播放
        preload:&#39;auto&#39;, // <video>加载元素后立即加载视频
        language:&#39;zh-CN&#39;,
        aspectRatio:&#39;16:9&#39;, //流畅模式,并计算播放器动态大小时使用该值
        fluid:true, //按比例缩放以适应容器
        sources:[{
         src:&#39;http://vjs.zencdn.net/v/oceans.mp4&#39;,
         type:&#39;video/mp4&#39;
        }],
        poster:&#39;http://vjs.zencdn.net/v/oceans.png&#39;, // 封面地址
        notSupportedMessage:&#39;此视频暂无法播放,请稍后再试&#39;,
      }
    }
  }
### ######Code address: https://github.com/yinglichen/videoPlayer######ps: I used canvas to write a subtitle function, which still needs to be repaired and will be added later. ######Recommended: "###The latest 5 vue.js video tutorial selections###"###

The above is the detailed content of How to add video in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn