天天看點

H5播放HLS視訊流(.m3u8)

HLS視訊流在移動端直接就可以通過video标簽播放,在PC端則需要通過擴充插件進行解碼才能播放

這裡使用hls.js進行解碼,相關文檔請看 https://www.npmjs.com/package/hls.js/v/canary

<!DOCTYPE html>
<html >

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hls視訊流播放</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            overflow: hidden;
            text-align: center;
        }
    </style>
</head>

<body>
    <video id="video" autoplay controls></video>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    <script>
        var video = document.getElementById('video');
        var videoSrc = 'http://***/abc.m3u8';

        if (video.canPlayType('application/vnd.apple.mpegurl')) {
            // 浏覽器本身支援HLS
            video.src = videoSrc;
        } else if (Hls.isSupported()) {
            // 檢查是否支援HLS.js
            var hls = new Hls();
            hls.loadSource(videoSrc);
            hls.attachMedia(video);
        }
    </script>

</body>

</html>
           

繼續閱讀