找回密码
 立即注册

Vue结合Video.js播放m3u8视频流的方法示例

[复制链接]
发表于 2019-5-20 14:50:26 | 显示全部楼层 |阅读模式

npm install --save video.jsnpm 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 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节点初始化播放器:


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 href="https://unpkg.com/video.js/dist/video-js.css">  <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 controls preload="auto"   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 aria-live="polite" aria-expanded="false" aria-haspopup="true">'   + '<div role="presentation">'   + '<ul role="menu">'   + '<li tabindex="-1" role="menuitemcheckbox">普通</li>'   + '<li tabindex="-1" role="menuitemcheckbox">标清 </li>'   + '<li tabindex="-1" role="menuitemcheckbox">高清 </li>'   + '</ul></div>'   +' <button type="button" aria-live="polite" title="清晰度切换" aria-disabled="false">'   +'  <span aria-hidden="true"></span><span>清晰度切换</span>'   +' </button>'   +'</div>'   );


 楼主| 发表于 2019-5-20 14:50:26 | 显示全部楼层

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>

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|笑鹰技术博客 ( 闽ICP备17023750号-1 )

GMT+8, 2024-11-21 22:25 , Processed in 0.093943 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表