溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Vue插入視頻的2種方法小結(jié)

發(fā)布時(shí)間:2020-09-18 02:19:40 來源:腳本之家 閱讀:552 作者:1994陳 欄目:web開發(fā)

基于Vue插入視頻的2種方法小結(jié)

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

方法一:iframe插入視頻鏈接

1.1 ##### 當(dāng)前播放的視頻

<div class="video-wrap" >
     <iframe :src="this.activeVideo.youtobeURL" frameborder='0'
     allow='autoplay;encrypted-media' allowfullscreen style='width:100%;height:500px;'>
     </iframe>
     <h4>{{this.activeVideo.title}}</h4>
    </div>

1.2#####視頻列表

<div class="video-list" >
    <div v-for='video in videos' :key='video.id' class="thumbnail" >
     <div class="thumbnail-img" >
      <div 
      @click="activeVideoShow(video.id)"></div>
     <iframe :src='video.youtobeURL' :alt="video.title" />
     </div>
     <div class="thumbnail-info">
      <h5>{{video.title}}</h5>
      <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#####定義的數(shù)據(jù)結(jié)構(gòu)(自己寫的demo,可能實(shí)際中后臺返的數(shù)據(jù)結(jié)構(gòu)會有所不同)

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##### 點(diǎn)擊視頻列表中的視頻轉(zhuǎn)變?yōu)楫?dāng)前視頻

ps:最開始的時(shí)候把點(diǎn)擊事件寫在iframe上,但是點(diǎn)擊無效。后來寫了個(gè)div,完美解決:

<div 
      @click="activeVideoShow(video.id)"></div>

1.5#####轉(zhuǎn)換當(dāng)前視頻的點(diǎn)擊事件:通過id來判斷當(dāng)前點(diǎn)擊的是哪個(gè)

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

方法二:引用了vue-video-player插件(沒有視頻列表)

相對于iframe方法寫了一堆div和style,vue-video-player簡直精簡到起飛

2.1#####第一次用這個(gè)插件,不是很熟悉,所以根據(jù)官方的API 寫了一個(gè)videoPlayer的組件,代碼如下:

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

2.1-1#####需要引入video.js和定義相關(guān)的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#####定義相關(guān)數(shù)據(jù)

props:{
   state:Boolean,
  },
data(){
    return{
      videoOptions:{
        playbackRates:[1.0,1.5,2.0], // 播放速度
        autoplay:false, // 如果true,瀏覽器準(zhǔn)備好時(shí)開始回放
        controls:true,
        muted:false, // 默認(rèn)情況下將會消除任何音頻
        loop:false, //循環(huán)播放
        preload:'auto', // <video>加載元素后立即加載視頻
        language:'zh-CN',
        aspectRatio:'16:9', //流暢模式,并計(jì)算播放器動態(tài)大小時(shí)使用該值
        fluid:true, //按比例縮放以適應(yīng)容器
        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寫了個(gè)字幕功能,還有待修繕,后期補(bǔ)上。

總結(jié)

以上所述是小編給大家介紹的基于Vue插入視頻的2種方法小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI