屏幕快照 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種方法小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
|
新聞熱點
疑難解答