首页  >  文章  >  web前端  >  vuejs如何添加视频

vuejs如何添加视频

藏色散人
藏色散人原创
2021-11-01 14:47:205497浏览

vuejs添加视频的方法:1、通过iframe插入视频链接;2、通过引用vue-video-player插件实现添加视频即可。

vuejs如何添加视频

本文操作环境:windows7系统、vue2.9.6版,DELL G3电脑。

vuejs如何添加视频?

基于Vue插入视频的2种方法小结:


方法一:iframe插入视频链接

1.1 ##### 当前播放的视频

   

<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#####视频列表

   

<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#####定义的数据结构(自己写的demo,可能实际中后台返的数据结构会有所不同)

   

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##### 点击视频列表中的视频转变为当前视频

ps:最开始的时候把点击事件写在iframe上,但是点击无效。后来写了个div,完美解决:

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

   

1.5#####转换当前视频的点击事件:通过id来判断当前点击的是哪个

   

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

   

方法二:引用了vue-video-player插件(没有视频列表)

相对于iframe方法写了一堆div和style,vue-video-player简直精简到起飞

2.1#####第一次用这个插件,不是很熟悉,所以根据官方的API 写了一个videoPlayer的组件,代码如下:

   

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

   

2.1-1#####需要引入video.js和定义相关的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#####在插入视频的页面中引入上面的videoPlayer组件,在view层代码如下:

   

<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#####需要引入的插件

   

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#####定义相关数据

   

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;,
      }
    }
  }

   

代码地址: https://github.com/yinglichen/videoPlayer

ps:用canvas写了个字幕功能,还有待修缮,后期补上。

推荐:《最新的5个vue.js视频教程精选

以上是vuejs如何添加视频的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn