基於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寫了個字幕功能,還有待修繕,後期補上。