天天看点

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>
           

继续阅读