Vue結(jié)合Video.js播放m3u8視頻流的方法示例
首先,我們需要在vue工程中安裝video.js相關(guān)依賴。
npm install --save video.js npm install --save videojs-contrib-hls
然后,我們需要引入videojs的css文件,例如在main.js中引入
import 'video.js/dist/video-js.css'
接著,我們在需要播放視頻的頁面引入js對象
import videojs from 'video.js' import 'videojs-contrib-hls'
指定一個video容器,例如:
<video id="my-video" class="video-js vjs-default-skin" controls preload="auto" poster="../assets/video.png"> <source src="http://127.0.0.1:7086/aaa/213/stream/1.m3u8" type="application/x-mpegURL"> </video>
最后,我們在mounted節(jié)點初始化播放器:
videojs('my-video', { bigPlayButton: false, textTrackDisplay: false, posterImage: true, errorDisplay: false, controlBar: true }, function () { this.play() })
Video.js中m3u8視頻清晰度切換
完成測試代碼
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>視頻控制</title> <link rel="external nofollow" rel="stylesheet"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script> <script src="https://unpkg.com/video.js/dist/video.js"></script> <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script> </head> <body> <video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="640" height="268" data-setup='{}'> <source src="http://localhost/video/c/1928.m3u8" type="application/x-mpegURL"> </video> <br/> </body> <script type="text/javascript"> window.onload=function(){ var videoPanelMenu = $(".vjs-fullscreen-control"); videoPanelMenu.before('<div class="vjs-subs-caps-button vjs-menu-button vjs-menu-button-popup vjs-control vjs-button" aria-live="polite" aria-expanded="false" aria-haspopup="true">' + '<div class="vjs-menu" role="presentation">' + '<ul class="vjs-menu-content" role="menu">' + '<li class="vjs-menu-item" tabindex="-1" role="menuitemcheckbox" onclick="changeVideo(1)">普通</li>' + '<li class="vjs-menu-item" tabindex="-1" role="menuitemcheckbox" onclick="changeVideo(2)">標(biāo)清 </li>' + '<li class="vjs-menu-item" tabindex="-1" role="menuitemcheckbox" onclick="changeVideo(3)">高清 </li>' + '</ul></div>' +' <button class="vjs-subs-caps-button vjs-control vjs-button" type="button" aria-live="polite" title="清晰度切換" aria-disabled="false">' +' <span aria-hidden="true" class="vjs-icon-placeholder"></span><span class="vjs-control-text">清晰度切換</span>' +' </button>' +'</div>' ); var obj={tag:false,ctime:0}; window.obj=obj; var myPlayer=videojs.getPlayers()['my_video_1']; myPlayer.on("timeupdate", function(){ if(window.obj.tag){ videojs("my_video_1").currentTime(window.obj.ctime) videojs("my_video_1").play(); window.obj.tag=false; } //視頻打點 var ctime_=videojs("my_video_1").currentTime().currentTime(); if(ctime_==60){ videojs("my_video_1").currentTime(ctime_+1); //do something } }); } function changeVideo(type){ var ctime=videojs("my_video_1").currentTime(); if(type==1){ videojs("my_video_1").src([{type: "application/x-mpegURL", src: "http://localhost/video/LD/1928.m3u8" }]); videojs("my_video_1").play(); } if(type==2){ videojs("my_video_1").src([{type: "application/x-mpegURL", src: "http://localhost/video/C/1928.m3u8" }]); videojs("my_video_1").play(); } if(type==3){ videojs("my_video_1").src([{type: "application/x-mpegURL", src: "http://localhost/video/HD/1928.m3u8" }]); videojs("my_video_1").play(); } window.obj.ctime=ctime; window.obj.tag=true; } </script> </html>
以上就是如題最簡單的實現(xiàn),更多需求請自行閱讀video.js相關(guān)api或者持續(xù)關(guān)注。也希望大家多多支持腳本之家。
相關(guān)文章
Vue生產(chǎn)和開發(fā)環(huán)境如何切換及過濾器的使用
本文主要介紹了Vue生產(chǎn)、開發(fā)環(huán)境如何切換及過濾器的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08詳解vue-router 2.0 常用基礎(chǔ)知識點之導(dǎo)航鉤子
本篇文章主要介紹了vue-router 2.0 常用基礎(chǔ)知識點之導(dǎo)航鉤子,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05vite+vue3+tsx項目打包后動態(tài)路由無法加載頁面的問題及解決
這篇文章主要介紹了vite+vue3+tsx項目打包后動態(tài)路由無法加載頁面的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Vue3之getCurrentInstance與ts結(jié)合使用的方式
這篇文章主要介紹了Vue3之getCurrentInstance與ts結(jié)合使用的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04用electron 打包發(fā)布集成vue2.0項目的操作過程
這篇文章主要介紹了用electron 打包發(fā)布集成vue2.0項目的操作步驟,把electron 加入到自己項目中各種不兼容,升級版本踩坑無數(shù)個,今天通過本文給大家分享下詳細(xì)過程,需要的朋友可以參考下2022-10-10解決Vue2跨域報錯AxiosError?net::ERR_FAILED、?Network?Error、ERR_N
這篇文章主要給大家介紹了關(guān)于解決Vue2跨域報錯AxiosError?net::ERR_FAILED、?Network?Error、ERR_NETWORK的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11