最近遇到了在web上播放直播的需求,直播可支持m3u8视频格式,所以尝试了一下在html中播放m3u8视频,内容如下
值得注意的是我们需要依赖hls.js
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<style>
html, body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- <video id="video" controls width="100%" height="211"></video> -->
<video x5-video-player-type="h5-page" x5-video-player-fullscreen="true" preload="auto" controls controlslist="nodownload" style="width: 100%; height: 211px;" playsinline="" webkit-inline="" webkit-playsinline="" x-webkit-airplay="allow" height="211" id="video"></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
var video = document.getElementById('video');
if(Hls.isSupported()) {
var hls = new Hls();
// hls.loadSource('https://yunqivedio.alicdn.com/2017yq/v2/0x0/96d79d3f5400514a6883869399708e11/96d79d3f5400514a6883869399708e11.m3u8');
hls.loadSource('http://v3bj.weizan.cn/11891857/192943029237849739/replay.1612264946.67870977.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// video.src = 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8';
video.src = 'http://v3bj.weizan.cn/11891857/192943029237849739/replay.1612264946.67870977.m3u8';
video.addEventListener('loadedmetadata',function() {
video.play();
});
}
</script>
</body>
</html>