vue+node 實現(xiàn)視頻在線播放的實例代碼
1.node服務端
數(shù)據(jù)流傳輸,可在線緩存
//獲取參數(shù)
var params=urldeal.parse(req.url,true).query
const ROOT_PATH = process.cwd();//必須使用絕對路徑,使用相對路徑會直接下載文件
let path =ROOT_PATH+params.url;
let stat = fs.statSync(path); //獲取文件信息
let fileSize = stat.size;
let range = req.headers.range;
if (range) {
//有range頭才使用206狀態(tài)碼
let parts = range.replace(/bytes=/, "").split("-");
let start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : start + 9999999;
// end 在最后取值為 fileSize - 1
end = end > fileSize - 1 ? fileSize - 1 : end;
let chunksize = (end - start) + 1;
let file = fs.createReadStream(path, { start, end });
let head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
res.writeHead(206, head);
file.pipe(res); //Node中的Server端HTTP response是Writable流
} else {
let head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
res.writeHead(200, head);
fs.createReadStream(path).pipe(res);
}
2.vue客戶端
1.安裝video-player插件
cnpm install vue-video-player --save
2.組件中引用
<video-player class="video-player vjs-custom-skin"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions"
></video-player>
3.調(diào)用的data中的數(shù)據(jù)
data() {
return {
// 視頻播放
playerOptions: {
playbackRates: [0.5, 1.0, 1.5, 2.0], //播放速度
autoplay: false, //如果true,瀏覽器準備好時開始回放。
muted: false, // 默認情況下將會消除任何音頻。
loop: false, // 導致視頻一結束就重新開始。
preload: 'auto', // 建議瀏覽器在<video>加載元素后是否應該開始下載視頻數(shù)據(jù)。auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)
language: 'zh-CN',
aspectRatio: '16:9', // 將播放器置于流暢模式,并在計算播放器的動態(tài)大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數(shù)字(例如"16:9"或"4:3")
fluid: true, // 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。
sources: [{
type: "",
//src: require('@/assets/'+this.vurl)//url地址
src: 'http://localhost:10086/videos?url=/public/videos/'+this.vurl, //url地址,請求中需要包含具體的視頻文件名
}],
poster: '', //你的封面地址
// width: document.documentElement.clientWidth,
notSupportedMessage: '此視頻暫無法播放,請稍后再試', //允許覆蓋Video.js無法播放媒體源時顯示的默認信息。
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true //全屏按鈕
}
}
}
},
附錄:vue+flvjs播放直播流FLV,分頁時如何斷開之前直播流解決辦法
使用flvjs庫同時播放flv文件,需要分頁發(fā)現(xiàn),之前直播流沒有斷開,很影響性能,網(wǎng)上查閱借鑒下邊代碼實現(xiàn)斷開上頁直播流
// 銷毀播放器實例
closePlayer() {
if (this.player.length > 0) {
this.player.forEach((item) => {
item.destroy(true);
item.off('ended', function () {});
item.off('error', function () {});
item = null;
});
this.player = [];
}
},
// 初始化播放器
initPlayer(id, url, img) {
let doms = document.getElementById(id);
doms.innerHTML = '';
this.player.push(
new FlvPlayer({
id: id,
url: url,
poster: img,
isLive: true,
autoplay: true,
volume: id == 'videos0' ? 0.5 : 0,
preloadTime: 10,
minCachedTime: 5,
cors: true,
})
);
this.player[this.player.length - 1].on('ended', () => {
//事件名稱可以在上述查詢
this.getLivingList();
});
this.player[this.player.length - 1].on('error', () => {
//事件名稱可以在上述查詢
this.getLivingList();
});
},
// 翻頁操作
currentChange(e) {
this.closePlayer();
this.pageno = e;
this.getLivingList();
},
// 獲取直播中的列表
async getLivingList() {
let res = await this.$req(api.liveShowers, {
...this.searchForm,
pageno: this.pageno - 1,
pagesize: this.pagesize,
});
console.log(res);
if (res.code == 200) {
// 獲取到數(shù)據(jù),賦值然后循環(huán)實例化
res.data.showers.push(res.data.showers[0]);
res.data.showers.push(res.data.showers[0]);
res.data.showers.push(res.data.showers[0]);
res.data.showers.push(res.data.showers[0]);
this.livingList = res.data.showers;
this.totalP = res.data.total_page - 0;
this.totalS = res.data.total_showers;
this.isrefresh = false;
if (this.livingList.length > 0) {
setTimeout(() => {
this.livingList.forEach((item, idx) => {
let domid = 'videos' + idx;
this.initPlayer(
domid,
item.play_url,
this.$store.state.coverTop + item.showcover
);
});
}, 400);
} else {
// 如果返回沒有直播數(shù)據(jù)又不是第一頁。就跳到第一頁再請求一下
if (this.pageno > 1) {
this.pageno = 1;
this.getLivingList();
}
}
}
},
總結
到此這篇關于vue+node 實現(xiàn)視頻在線播放的實例代碼的文章就介紹到這了,更多相關vue視頻在線播放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- vue video和vue-video-player實現(xiàn)視頻鋪滿教程
- vue-video-player視頻播放器使用配置詳解
- vue項目中播放rtmp視頻文件流的方法
- vue實現(xiàn)移動端input上傳視頻、音頻
- vue-video-player實現(xiàn)實時視頻播放方式(監(jiān)控設備-rtmp流)
- vue結合el-upload實現(xiàn)騰訊云視頻上傳功能
- vue項目中自定義video視頻控制條的實現(xiàn)代碼
- vue中上傳視頻或圖片或圖片和文字一起到后端的解決方法
- vue視頻播放插件vue-video-player的具體使用方法
- vue vantUI實現(xiàn)文件(圖片、文檔、視頻、音頻)上傳(多文件)
- Vue登錄主頁動態(tài)背景短視頻制作
- vue通過video.js解決m3u8視頻播放格式的方法
- Vue結合Video.js播放m3u8視頻流的方法示例
- 如何在vue中使用video.js播放m3u8格式的視頻
相關文章
windows下vue.js開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細介紹了windows下vue.js開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Vue2.x如何解決Element組件el-tooltip滾動時錯位不消失的問題
這篇文章主要介紹了Vue2.x如何解決Element組件el-tooltip滾動時錯位不消失的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vue3 Element Plus中icon圖標不顯示的解決方案
這篇文章主要介紹了vue3 Element Plus中icon圖標不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue 動態(tài)添加class,三個以上的條件做判斷方式
這篇文章主要介紹了vue 動態(tài)添加class,三個以上的條件做判斷方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue中使用elementUI自定義校驗及點擊提交不生效問題解決辦法
我們在項目中經(jīng)常會用到ElementUI的表單驗證,下面這篇文章主要給大家介紹了關于vue中使用elementUI自定義校驗及點擊提交不生效問題解決的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12

