news 2026/5/26 5:50:58

使用Vue播放M3U8视频流的方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
使用Vue播放M3U8视频流的方法

使用Vue播放M3U8视频流的方法

安装依赖
需要安装video.js和videojs-contrib-hls插件,用于解析和播放M3U8格式的视频流。

npm install video.js videojs-contrib-hls

引入并初始化Video.js
在Vue组件中引入Video.js及相关样式,初始化播放器并配置HLS支持。

importvideojsfrom'video.js'import'video.js/dist/video-js.css'import'videojs-contrib-hls'exportdefault{mounted(){this.initVideoPlayer()},methods:{initVideoPlayer(){this.player=videojs(this.$refs.videoPlayer,{autoplay:true,controls:true,sources:[{src:'your-m3u8-url.m3u8',type:'application/x-mpegURL'}]})}},beforeDestroy(){if(this.player){this.player.dispose()}}}

模板部分
在模板中添加video标签作为播放器容器,需设置data-setup属性为空对象以启用Video.js初始化。

<template><div><videoref="videoPlayer"class="video-js vjs-default-skin"width="640"height="360"data-setup="{}"></video></div></template>

样式调整

.video-js{width:100%;max-width:640px;margin:0 auto;}

通过CSS调整播放器尺寸和外观,确保适应页面布局。

注意事项
确保M3U8视频流地址可跨域访问,或配置服务器CORS策略。
移动端可能需要添加playsinline属性以实现内联播放。
若使用HTTPS环境,需确保视频流地址同为HTTPS。

完整组件示例

<template><divclass="video-container"><videoref="videoPlayer"class="video-js"></video></div></template><script>importvideojsfrom'video.js'import'video.js/dist/video-js.css'import'videojs-contrib-hls'exportdefault{props:{src:{type:String,required:true}},mounted(){this.initPlayer()},methods:{initPlayer(){this.player=videojs(this.$refs.videoPlayer,{autoplay:false,controls:true,sources:[{src:this.src,type:'application/x-mpegURL'}]})}},beforeDestroy(){if(this.player){this.player.dispose()}}}</script><stylescoped>.video-container{margin:20px auto;width:80%;}</style>

替代方案(使用hls.js)
若需更轻量级方案,可使用hls.js库直接处理M3U8流。

安装hls.js

npminstallhls.js

实现代码

importHlsfrom'hls.js'exportdefault{data(){return{hls:null}},mounted(){this.loadVideo()},methods:{loadVideo(){constvideoSrc='your-m3u8-url.m3u8'constvideo=this.$refs.videoPlayerif(Hls.isSupported()){this.hls=newHls()this.hls.loadSource(videoSrc)this.hls.attachMedia(video)}elseif(video.canPlayType('application/vnd.apple.mpegurl')){video.src=videoSrc}}},beforeDestroy(){if(this.hls){this.hls.destroy()}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/25 16:23:45

功能强大的微信表单系统,让数据收集与管理变得如此简单!

温馨提示&#xff1a;文末有资源获取方式核心功能与卓越特点一览&#xff1a;极致的通用性与灵活性&#xff1a;系统支持无限创建独立的表单主题&#xff0c;满足您所有业务场景下的数据收集需求&#xff0c;从简单的信息登记到复杂的多步骤申请&#xff0c;都能轻松驾驭。源码…

作者头像 李华
网站建设 2026/5/25 1:59:56

操作便捷、功能强大的智能表单管理系统源码,支持在微信端使用语音进行回复与沟通

温馨提示&#xff1a;文末有资源获取方式一款能深度融入微信、操作便捷、功能强大的智能表单管理系统&#xff0c;无疑是提升业务响应速度与服务质量的关键。我们倾力打造的新一代表单系统&#xff0c;特别强化了移动端管理与智能交互体验&#xff0c;旨在帮助您将微信端的业务…

作者头像 李华
网站建设 2026/5/26 3:27:15

快速上手Forge.js:JavaScript加密与TLS通信的终极指南

快速上手Forge.js&#xff1a;JavaScript加密与TLS通信的终极指南 【免费下载链接】forge A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps 项目地址: https://gitcode.com/gh_mirrors/for/forge 在当今数字化…

作者头像 李华
网站建设 2026/5/24 21:07:10

吉时利2430 keithley2430数字源表 技术

keithley2430脉冲数字源表吉时利SourceMeter&#xff08;数字源表&#xff09;系列是专为那些要求紧密结合激励源和测量功能&#xff0c;要求精密电压源并同时进行电流与电压测量的测试应用而设计的。所有源表均由一个精密的、低噪声、高稳定的带回读功能的直流电源和一个低噪声…

作者头像 李华
网站建设 2026/5/26 1:16:02

小米 MiMo-V2-Flash 快速接入 iFlow-CLI 保姆级教程

教程 1、登录小米开放者平台&#xff0c;并进入控制台&#xff1a;Xiaomi MiMo 开放平台 2、点击左侧“API-Keys”栏&#xff0c;再创建API-Key 3、启动iFlow-CLI&#xff0c;输入指令/auth&#xff0c;选择第三种登录方式&#xff1a;“OpenAI 兼容 API” 4、在base URL输入框…

作者头像 李华