hls視頻播放-web視頻播放


由於非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/hls.js@latest"></script>
<!-- Or if you want a more recent canary version -->
<!-- <script src="https://cdn.jsdelivr.net/npm/hls.js@canary"></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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM