基于Vue插入視頻的2種方法小結(jié)
屏幕快照 2019-04-01 下午8.06.02.png
方法一:iframe插入視頻鏈接
1.1 ##### 當(dāng)前播放的視頻
<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#####定義的數(shù)據(jù)結(jié)構(gòu)(自己寫的demo,可能實(shí)際中后臺(tái)返的數(shù)據(jù)結(jié)構(gòu)會(huì)有所不同)
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:最開(kāi)始的時(shí)候把點(diǎn)擊事件寫在iframe上,但是點(diǎn)擊無(wú)效。后來(lái)寫了個(gè)div,完美解決:
<div style="height:50%;width:100%;position:absolute;z-index:999" @click="activeVideoShow(video.id)"></div>
1.5#####轉(zhuǎn)換當(dāng)前視頻的點(diǎn)擊事件:通過(guò)id來(lái)判斷當(dāng)前點(diǎn)擊的是哪個(gè)
activeVideoShow(id){ this.videos.filter(item=>{ if(id == item.id){ this.activeVideo=item } }) }
方法二:引用了vue-video-player插件(沒(méi)有視頻列表)
相對(duì)于iframe方法寫了一堆div和style,vue-video-player簡(jiǎn)直精簡(jiǎn)到起飛
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#####在插入視頻的頁(yè)面中引入上面的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í)開(kāi)始回放 controls:true, muted:false, // 默認(rèn)情況下將會(huì)消除任何音頻 loop:false, //循環(huán)播放 preload:'auto', // <video>加載元素后立即加載視頻 language:'zh-CN', aspectRatio:'16:9', //流暢模式,并計(jì)算播放器動(dòng)態(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:'此視頻暫無(wú)法播放,請(qǐng)稍后再試', } } }
代碼地址: https://github.com/yinglichen/videoPlayer
ps:用canvas寫了個(gè)字幕功能,還有待修繕,后期補(bǔ)上。
總結(jié)
以上所述是小編給大家介紹的基于Vue插入視頻的2種方法小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue?element表格某一列內(nèi)容過(guò)多,超出省略號(hào)顯示的實(shí)現(xiàn)
這篇文章主要介紹了vue?element表格某一列內(nèi)容過(guò)多,超出省略號(hào)顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01vscode+vue cli3.0創(chuàng)建項(xiàng)目配置Prettier+eslint方式
這篇文章主要介紹了vscode+vue cli3.0創(chuàng)建項(xiàng)目配置Prettier+eslint方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue Echarts實(shí)現(xiàn)圖表輪播圖以及圖表組件封裝和節(jié)流函數(shù)優(yōu)化講解
這篇文章主要介紹了在Vue中優(yōu)雅的使用Echarts實(shí)現(xiàn)圖表輪播圖、Echarts圖表組件封裝、節(jié)流函數(shù)優(yōu)化圖表性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01關(guān)于element-ui?單選框默認(rèn)值不選中的解決
這篇文章主要介紹了關(guān)于element-ui?單選框默認(rèn)值不選中的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼
本文主要介紹了vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05