hls视频播放-基于ChPlayer

Posted dpwow

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hls视频播放-基于ChPlayer相关的知识,希望对你有一定的参考价值。

由于非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>

 

以上是关于hls视频播放-基于ChPlayer的主要内容,如果未能解决你的问题,请参考以下文章

FFmpeg基于HLS实现大视频分片下载播放[视频直播二]

FFmpeg基于HLS实现大视频分片下载播放[视频直播二]

使用主播放列表时,HLS.js 一次加载所有子片段

在 Android TV 上升级 Android WebView 后,hls 264 视频不再播放

HLS.js 获取视频片段信息

AVPlayer 在在线模式下停止播放 AES 加密的离线 HLS 视频