由于非Safari浏览器不能直接支持hls视频播放,使用Html5的video标签嵌入视频文件进行播放,由于video标签只支持MP4、WebM、Ogg等格式,
无法加载hls的m3u8格式文件,不能用在chrome,ie等浏览器上。
基于ChPlayer 视频播放
经过查询资料发现ChPlayer,使用ChPlayer进行视频的播放,支持点播和直播,支持m3u8格式。
调用示例:
<script type="text/javascript"> var videoObject = { logo: ‘chplayer‘, //设置logo,非必须 container: ‘#video‘,//“#”代表容器的ID,“.”或“”代表容器的class variable: ‘player‘,//该属性必需设置,值等于下面的new chplayer()的对象 video:‘examples01.mp4‘//视频地址 }; var player=new chplayer(videoObject); </script>
基于hls.js 视频播放
html5浏览器实现hls视频播放的兼容解决方案hls.js。兼容IE8/IE11/edge/Chrome/火狐。
github传送门:https://github.com/video-dev/hls.js
官方示例:
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> <!-- Or if you want a more recent canary version --> <!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> --> <video id="video"></video> <script> var video = document.getElementById(‘video‘); if(Hls.isSupported()) { var hls = new Hls(); hls.loadSource(‘https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8‘); hls.attachMedia(video); hls.on(Hls.Events.MANIFEST_PARSED,function() { video.play(); }); } // hls.js is not supported on platforms that do not have Media Source Extensions (MSE) enabled. // When the browser has built-in HLS support (check using `canPlayType`), we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video element throught the `src` property. // This is using the built-in support of the plain video element, without using hls.js. // Note: it would be more normal to wait on the ‘canplay‘ event below however on Safari (where you are most likely to find built-in HLS support) the video.src URL must be on the user-driven // white-list before a ‘canplay‘ event will be emitted; the last video event that can be reliably listened-for when the URL is not on the white-list is ‘loadedmetadata‘. else if (video.canPlayType(‘application/vnd.apple.mpegurl‘)) { video.src = ‘https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8‘; video.addEventListener(‘loadedmetadata‘,function() { video.play(); }); } </script>
原文地址:https://www.cnblogs.com/dpwow/p/9592302.html
时间: 2024-10-29 00:40:23