Vue.js 使用 vue-video-player 視頻播放器(最全面)

前端效果:

1.首先要安裝 vue-video-player 包

npm install vue-video-player --save

2.在 main.js 中進行配置

import VueVideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
Vue.use(VueVideoPlayer);

3.代碼中使用

<template>
	<video-player
		class="video-player vjs-custom-skin"
			  ref="videoPlayer"
			  :playsinline="true"
			  :options="playerOptions"/>
</template>

注意:class="video-player vjs-custom-skin" 這個必須寫,不寫無法出現上面完成效果!!!

4.參數配置:playerOptions中的內容

playerOptions: {
                    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: true, // 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。
                    sources: [{
                        type: "video/mp4",// 這裏的種類支持很多種:基本視頻格式、直播、流媒體等,具體可以參看git網址項目
                        src: "/api/project/getPlayResource/25" // url地址
                    }],
                    poster: "https://p1.music.126.net/5zs7IvmLv7KahY3BFzUmrg==/109951163635241613.jpg?param=600y500", // 你的封面地址
                    notSupportedMessage: '此視頻暫無法播放,請稍後再試', // 允許覆蓋Video.js無法播放媒體源時顯示的默認信息。
                    controlBar: {
                        timeDivider: true,
                        durationDisplay: true,
                        remainingTimeDisplay: false,
                        fullscreenToggle: true  // 全屏按鈕
                    }
                }

這個 playerOptions 是 data() 域中的內容

5.vue-video-player 其他 API

vue-video-player 是基於 video.js 實現的,所以 api 地址:https://docs.videojs.com/

調用的時候使用這個對象進行操作:this.$refs.videoPlayer.player

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章