How can I precisely detect HLS support on different browsers and different OS?

后端 未结 2 679
我寻月下人不归
我寻月下人不归 2021-01-14 15:08

According to this answer, to test the browser\'s capabilities to play HLS video, the MIME application/x-mpegURL can be used. But the problem with this approach

2条回答
  •  礼貌的吻别
    2021-01-14 15:43

    JavaScript version

    function supportsHLS() {
      var video = document.createElement('video');
      return Boolean(video.canPlayType('application/vnd.apple.mpegURL') || video.canPlayType('audio/mpegurl'))
    }
    

    Then use it as:

    if (supportsHLS()) {
      myVideoElement.src = 'your-hls-url.m3u8';
    } else {
      myVideoElement.src = 'your-plain-video-url.mp4';
    }
    

    HTML-only version (preferred)

    Let the browser pick the first source it supports. This is safer.

    
    

提交回复
热议问题