vue使用video外掛vue-video-player詳解
阿新 • • 發佈:2020-10-26
本文例項為大家分享了vue使用video外掛vue-video-player的具體程式碼,供大家參考,具體內容如下
進入我們的專案資料夾中,並開啟命令列視窗,然後進行下面的步驟:
1、安裝vue-video-player
輸入命令:
npm install vue-video-player -S
2、引入外掛
在專案的入口檔案main.js中引入外掛,如下:
import VideoPlayer from 'vue-video-player' require('video.js/dist/video-js.css') require('vue-video-player/src/custom-theme.css') Vue.use(VideoPlayer)
3、使用外掛
建立vue元件檔案VideoPlayer.vue,檔案內容如下:
<template> <div> <!-- 使用元件 --> <video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions" ></video-player> </div> </template> <script> // 匯入元件 import {videoPlayer} from 'vue-video-player' import 'videojs-flash' export default { name: 'VideoPlayer',components: { videoPlayer },data () { return { fileAreaHeight: 100,fileType: 'mp4',// 資源的型別 fileUrl: 'xxx' // 資源的路徑地址 } },computed: { playerOptions () { // 使用計算屬性 const playerOptionsObj = { techOrder: ['flash'],// 使用flase播放,可以播放flv格式的檔案 playbackRates: [0.7,1.0,1.5,2.0],//播放速度 autoplay: false,// 如果true,瀏覽器準備好時開始回放。 muted: false,// 預設情況下將會消除任何音訊。 loop: false,// 導致視訊一結束就重新開始。 // preload: 'auto',// 建議瀏覽器在<video>載入元素後是否應該開始下載視訊資料。auto瀏覽器選擇最佳行為,立即開始載入視訊(如果瀏覽器支援) language: 'zh-CN',// aspectRatio: '16:9',// 將播放器置於流暢模式,並在計算播放器的動態大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數字(例如"16:9"或"4:3") fluid: false,// 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。 sources: [{ type: 'video/' + this.fileType,// 資源格式寫法:'video/mp4',否則控制檯會出現notSupportedMessage設定的錯誤 src: this.fileUrl // url地址 }],poster: '',// 你的封面地址 // width: document.documentElement.clientWidth,height: this.fileAreaHeight,// 設定高度,fluid需要設定成flase notSupportedMessage: '此視訊暫無法播放...',// 允許覆蓋Video.js無法播放媒體源時顯示的預設資訊。 controlBar: { timeDivider: true,durationDisplay: true,remainingTimeDisplay: false,fullscreenToggle: true //全屏按鈕 } } return playerOptionsObj } } } </script> <style scoped> .video-js .vjs-big-play-button{ /*對播放按鈕的樣式進行設定*/ } </style>
注:
如果在VideoPlayer.vue中不匯入元件,則會報如下錯誤:
關於vue.js元件的教程,請大家點選專題vue.js元件學習教程進行學習。
更多vue學習教程請閱讀專題《vue實戰教程》
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。