js播放m3u8格式视频

引入 hls 和 jquery

代码片段

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>video</title>
	</head>
	<body>

		<button type="button" class="btn btn-success" id="bt" onclick="bt()">播放</button>
		<div style="width: 50%; height: 50%;">
			<video id="testVideo" src="" controls preload></video>
		</div>
		<!-- 引入jq -->
		<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
		<!-- 需要引入hls.js -->
		<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
		<script>
			$(function() {
				$('#testVideo').hide();
			});

			function bt() {
				loadVideo('https://v6.szjal.cn/20201212/ggJHXPox/index.m3u8');
				$('#bt').hide();
				$('#testVideo').show();
			}

			function loadVideo(vedio_url) {
				var video = document.getElementById('testVideo');
				if (Hls.isSupported()) {
					var hls = new Hls();
					hls.loadSource(vedio_url);
					hls.attachMedia(video);
					hls.on(Hls.Events.MANIFEST_PARSED, function() {
						video.play();
					});
				} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
					video.src = vedio_url;
					video.addEventListener('loadedmetadata', function() {
						video.play();
					});
				}
			}
		</script>
	</body>
</html>

上一篇:使用nginx搭建音视频点播服务器


下一篇:Leetcode56-合并区间