news 2026/7/14 13:16:55

Video.js播放m3u8格式视频流

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Video.js播放m3u8格式视频流

video.js的官网文档

https://docs.videojs.com/tutorial-vue.html
https://docs.videojs.com/tutorial-videojs.html

npm安装和引入

npm install --save video.js;
npm install --save videojs-contrib-hls

在main.js中引入 videojs的css文件

import ‘video.js/dist/video-js.css’

vue 使用

<video id="playercontainer"class="video-js vjs-default-skin"controls preload="auto"width="500px"height="500px"><!--<source src="https://cdn.letv-cdn.com/2018/12/05/JOCeEEUuoteFrjCg/playlist.m3u8"type="application/x-mpegURL"/>--><source:src="videoUrl"type="application/x-mpegURL"/></video><script>data(){return{player:null,videoUrl:'',}},methods:{getVideo(url){// console.log('视频完整url',url)this.player=videojs("playercontainer",{bigPlayButton:false,textTrackDisplay:false,posterImage:false,errorDisplay:false,controlBar:true,hls:{withCredentials:true},sources:[{type:'application/x-mpegURL',// src: 'https://cdn.letv-cdn.com/2018/12/05/JOCeEEUuoteFrjCg/playlist.m3u8', // 你的m3u8地址(必填)src:url// 你的m3u8地址(必填)}]},function(){this.play();});},queryVideo(){varparams={}checkVideo(params).then(res=>{this.videoUrl='http://123.123.20.5:'+res.data.data[0].urlthis.getVideo(this.videoUrl)})},}mounted(){this.getVideo();},// 实例销毁之前清除player方法,停止播放beforeDestroy(){// this.player.remove();//百度云视频if(this.player){this.player.dispose();//video.js}},// 实例销毁之后移除所有的监听器和完全销毁这些实例,清除它与其它实例的连接,解绑全部指令及监视器destroyed(){this.$off();this.$destroy();},<script>

切换视频操作

<button @click="changeVideo()">点击切换视频</button>changeVideo(){varmyPlayer=videojs("playercontainer");myPlayer.src([{type:"application/x-mpegURL",src:"http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8"}]);myPlayer.play();}

html使用

<linkhref="https://cdn.bootcss.com/video.js/6.3.3/video-js.min.css"rel="stylesheet"><scriptsrc="https://cdn.bootcss.com/video.js/6.3.3/video.min.js"></script><scriptsrc="https://cdn.bootcss.com/videojs-contrib-hls/5.11.0/videojs-contrib-hls.js"></script><videoid="singleVideo"width=540height=540class="video-js vjs-default-skin"controls><sourcesrc="**m3u8路径**"type="application/x-mpegURL"></video>/* src放的是视频的路径,type="application/x-mpegURL"不能少 */<scripttype="text/javascript">varplayer=videojs('singleVideo');player.play();</script>

百度云播放器 需要flash插件

http://cyberplayer.bcelive.com/demo/new/index.html

initVideo(url){// console.log(url)this.player=cyberplayer("playercontainer").setup({width:"100%",// 宽度,也可以支持百分比(不过父元素宽度要有)height:"100%",// 高度,也可以支持百分比title:"视频直播",// 标题isLive:true,// 必须设置,表明是直播视频file:'http://123.123.20.5:'+url,// 播放地址// file: 'http://123.123.20.5:83/openUrl/wVnq5EY/live.m3u8', // 播放地址// file: "http://cyberplayer.bcelive.com/videoworks/mda-kbuhu4wqdi08dwix/cyberplayer/mp4/cyberplayer-demo.mp4",autostart:true,// 是否自动播放stretching:"uniform",// 拉伸设置repeat:false,// 是否重复播放volume:100,// 音量controls:true,// controlbar是否显示barLogo:true,hls:{reconnecttime:5// hls直播重连间隔秒数},// rtmp: {// reconnecttime: 30, // rtmp直播的重连次数// bufferlength: 0 // 缓冲多少秒之后开始播放 默认1秒// },// primary: "flash", // 强制使用flash来播放,不设置的话则默认高优使用H5进行播放ak:"57ca2f8d164040c7ad4c06e385733fb9",// 公有云平台注册即可获得accessKey});},//需要 index引入<script type="text/javascript"src="static/lib/baiduVideo/cyberplayer.js"></script><script type="text/javascript"src="static/lib/baiduVideo/video.min.js"></script><script type="text/javascript"src="static/lib/baiduVideo/videojs-contrib-hls.min.js"></script>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/14 13:16:30

Tweepy深度解析:Python生态下的Twitter API终极解决方案

Tweepy深度解析&#xff1a;Python生态下的Twitter API终极解决方案 【免费下载链接】tweepy Twitter for Python! 项目地址: https://gitcode.com/gh_mirrors/tw/tweepy 在社交媒体数据分析和自动化开发领域&#xff0c;Tweepy作为Twitter官方推荐的Python SDK&#xf…

作者头像 李华
网站建设 2026/7/14 13:14:46

Windows系统下PostgreSQL数据库安装与pgAdmin图形化管理实战指南

1. PostgreSQL简介与Windows安装准备PostgreSQL是一款功能强大的开源关系型数据库&#xff0c;它支持SQL标准并提供了许多高级功能。作为开发者&#xff0c;选择PostgreSQL意味着你将获得一个稳定、可靠且功能丰富的数据库解决方案。在Windows上安装PostgreSQL前&#xff0c;需…

作者头像 李华
网站建设 2026/7/14 13:14:08

CSS实现7球转圈加载动画的完整指南

1. 项目概述&#xff1a;7球转圈加载动画的实现价值 作为前端开发中最常见的视觉反馈形式之一&#xff0c;加载动画直接影响用户等待时的心理感受。这个7个小球转圈圈的加载效果&#xff0c;通过CSS关键帧动画实现小球的位置和透明度变化&#xff0c;形成流畅的视觉循环。相比静…

作者头像 李华
网站建设 2026/7/14 13:13:44

C++ OpenCV开发资源包:实战指南与高效开发工作流构建

1. 项目概述&#xff1a;为什么你需要一个专属的C OpenCV开发资源包&#xff1f; 如果你正在用C和OpenCV做项目&#xff0c;无论是毕业设计、公司产品还是个人研究&#xff0c;大概率都经历过这样的场景&#xff1a;项目做到一半&#xff0c;突然需要一个特定的图像处理功能&a…

作者头像 李华