欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-01-11 10:49:55
屏幕快照 2019-04-01 下午8.06.02.png 方法一:iframe插入视频链接 1.1 ##### 当前播放的视频

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

屏幕快照 2019-04-01 下午8.06.02.png

方法一:iframe插入视频链接

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

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

1.2#####视频列表

<div class="video-list" style="float:right;width:20%;text-align:center;">
    <div v-for='video in videos' :key='video.id' class="thumbnail" >
     <div class="thumbnail-img" >
      <div style="height:50%;width:100%;position:absolute;z-index:999"
      @click="activevideoshow(video.id)"></div>
     <iframe :src='video.youtobeurl' :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:'test2',youtobeurl:'http://player.youku.com/embed/xmzcwnty3ntm2ma',speaker:'harry', likes:101,views:0,describe:'good'
   },{
    id:2,title:'test3',youtobeurl:'http://player.youku.com/embed/xmzcwnty3ntm2ma',speaker:'harry', likes:100,views:75,describe:'good'
   }],
   activevideo:{
    id:3,title:'test1',thumbnail:'./../../static/images/headimg.png',speaker:'harry', likes:0,views:0,describe:'good',
    youtobeurl:'http://player.youku.com/embed/xmzcwnty3ntm2ma'
   }
  }
 }

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 'video.js'
---------------------------------
props:{
    options:{
      type:object,
      default(){
        return{
        }
      }
    }
  },
data(){
    return{
      player:null
    }
  },
mounted(){
    this.player=videojs(this.$refs.videoplayer,this.options,function onplayerready(){
      console.log('onplayerready',this)
    })
  }

2.2#####在插入视频的页面中引入上面的videoplayer组件,在view层代码如下:

<video-player class="video-player vjs-custom-skin "
    ref="videoplayer"
    :playsinline='false'
    :options='videooptions'
    @play="onplayerplay($event)" 
    @pause='onplayerpause($event)'
    @statechagned='playerstatechanged($event)'
    >
    </video-player>

2.3#####需要引入的插件

import './../../node_modules/video.js/dist/video-js.css'
import './../../node_modules/vue-video-player/src/custom-theme.css'
import videojs from 'video.js'
import {videoplayer} from 'vue-video-player'
import 'videojs-flash'
import videoplayer from '@/components/videoplayer.vue'

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:'auto', // <video>加载元素后立即加载视频
        language:'zh-cn',
        aspectratio:'16:9', //流畅模式,并计算播放器动态大小时使用该值
        fluid:true, //按比例缩放以适应容器
        sources:[{
         src:'http://vjs.zencdn.net/v/oceans.mp4',
         type:'video/mp4'
        }],
        poster:'http://vjs.zencdn.net/v/oceans.png', // 封面地址
        notsupportedmessage:'此视频暂无法播放,请稍后再试',
      }
    }
  }

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

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

总结

以上所述是小编给大家介绍的基于vue插入视频的2种方法小结,希望对大家有所帮助