vue-video-player 斷點續播的實現
阿新 • • 發佈:2021-02-05
最近的專案中需要實現視訊斷點續播的功能,一聽到這個功能。內心方張了..但卻又有點小竊喜,小懵亂。抱著求學態度去挑戰一下。
1.安裝外掛
npm install vue-video-player --save
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.頁面使用元件
<el-tree :data="ChapterOptions" :props="defaultProps" node-key='id' highlight-current :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" /> <video-player ref="videoPlayer" class="video-player vjs-custom-skin" style="width: 1000px;height: 576px;display: inline-flex" :playsinline="true" :options="playerOptions" @pause="onPlayerPause($event)" @ended="onPlayerEnded($event)" @play="onPlayerPlay($event)" @timeupdate="onPlayerTimeupdate($event)" @ready="playerReadied" /> <script> import { videoPlayer } from 'vue-video-player' import 'video.js/dist/video-js.css' import 'vue-video-player/src/custom-theme.css' import { treeselect } from "@/api//driver/videoChapter"; import Treeselect from "@riophae/vue-treeselect"; import "@riophae/vue-treeselect/dist/vue-treeselect.css"; export default { name: "videoPlayer",components: { Treeselect,videoPlayer },data() { return { //使用者資訊 user:{},//=============================== paused: true,learningDuration: { userId: '',//使用者id chapterId:'',//章節id timeLog: '',//視訊觀看時長 },playerOptions: { playbackRates: [0.5,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: ''// url地址 } ],hls: true,poster: '',// 你的封面地址 width: document.documentElement.clientWidth,// 播放器寬度 notSupportedMessage: '此視訊暫無法播放,請稍後再試',// 允許覆蓋Video.js無法播放媒體源時顯示的預設資訊。 controlBar: { //當前時間和持續時間的分隔符 timeDivider: true,//顯示持續時間 durationDisplay: true,//是否顯示剩餘時間功能 remainingTimeDisplay: false,//全屏按鈕 fullscreenToggle: true } } }; },computed: { player() { return this.$refs.videoPlayer.player//自定義播放 } },mounted () { this.timer = setInterval(this.putLearningObj,3000) },destroyed () { // 如果定時器在執行則關閉 if (this.timer) { clearInterval(this.timer) } },methods: { //使用者資訊 getUser() { getUserProfile().then(response => { this.user = response.data; this.learningDuration.userId = this.user.userId }); },//============================ fullScreen() { const player = this.$refs.videoPlayer.player player.requestFullscreen()//呼叫全屏api方法 player.isFullscreen(true) player.play() },onPlayerPlay(player) { this.paused = false // player.play() },onPlayerPause (player) { this.paused = true // console.log('onPlayerPause!',player) },onPlayerEnded (player) { this.paused = false; // clearInterval(this.timer); },//當前播放位置發生變化時觸發。 onPlayerTimeupdate (player) { // console.log(' onPlayerTimeupdate!',this.timeLog) },/* 設定視訊進度 */ playerReadied: function (player) { },}; </script>
上面的 src視訊地址可以換成具體的地址串,也能換成後臺的地址串,因為我的是章節樹所以我和章節id進行了關聯
/** 查詢部門下拉樹結構 */ getTreeselect() { treeselect().then((response) => { //封面 var img = ''; this.ChapterOptions = response.data; for (let i = 0; i <this.ChapterOptions.length ; i++) { this.videoName = this.ChapterOptions[0].children[0].chapterName this.videoIntroduce = this.ChapterOptions[0].children[0].chapterIntroduce this.VideoUrl = JSON.parse(this.ChapterOptions[0].children[0].videoAddress) img = JSON.parse(this.ChapterOptions[0].children[0].imageAddress) //初始化封面 for (let j = 0; j <img.length ; j++) { this.playerOptions.poster =img[0]; } //初始化第一個章節視訊 for (let j = 0; j <this.VideoUrl.length ; j++) { this.playerOptions.sources[0].src = this.VideoUrl[0] } //初始化章節 this.learningDuration.chapterId = this.ChapterOptions[0].children[0].id; //預設高亮第一個章節節點 this.$nextTick(()=>{ this.$refs.tree.setCurrentKey(this.ChapterOptions[0].children[0].id); }) } }); },// 篩選節點 filterNode(value,data) { if (!value) return true; return data.label.indexOf(value) !== -1; },// 節點單擊事件 handleNodeClick(data) { // console.log(data) var img = ''; //重新整理原視訊,原封面 this.playerOptions.sources[0].src = ''; this.playerOptions.poster = ''; //轉換視訊 this.VideoUrl= JSON.parse(data.videoAddress); // console.log("this.VideoUrl") for (let i = 0; i <this.VideoUrl.length ; i++) { this.playerOptions.sources[0].src = this.VideoUrl[0]; } img = JSON.parse(data.imageAddress); for (let i = 0; i <img.length ; i++) { this.playerOptions.poster = img[0]; } // console.log("this.playerOptions.sources[0].src") // console.log(this.playerOptions.sources[0].src) //章節介紹 this.videoIntroduce = data.chapterIntroduce; //章節名稱 this.videoName = data.chapterName; //章節id this.learningDuration.chapterId = data.id // console.log(this.videoIntroduce) },
4.進度儲存
接下來就是 儲存視訊的進度條了,通過打印發現onPlayerTimeupdate可獲取到視訊的進度,故採用定時器 每3秒觸發一次資料互動
computed: { player() { return this.$refs.videoPlayer.player//自定義播放 } },methods: { putLearningObj () { if (!this.paused) { //儲存視訊進度 saveTime(this.learningDuration) console.log('putLearningObj ~~~~~~~~~') } },//當前播放位置發生變化時觸發。 onPlayerTimeupdate (player) { this.learningDuration.timeLog = player.cache_.currentTime // console.log(' onPlayerTimeupdate!',},
saveTime是我自定義的與後端互動的方法。(可自行定義)
// 儲存視訊進度 export function saveTime(data) { return request({ url: '/***/****/***/',method: 'put',data: data }) }
那麼到了這一步 進度就能儲存下來了
4.進度恢復
想要恢復進度,就必須在視訊播放前把 儲存進度下來的設定到視訊當中,通過列印可以看出playerReadied 可以設定
/* 設定視訊進度 */ playerReadied: function (player) { //可在此呼叫後臺互動方法 ... player.currentTime(this.learningDuration.timeLog) },
到此 進度可以 恢復了 大功告成!。至於後臺互動資料 需求不一樣,程式碼也就沒有貼出來。
到此這篇關於vue-video-player 斷點續播的文章就介紹到這了,更多相關vue video player 斷點續播內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!