其他分享
首页 > 其他分享> > js播放m3u8格式视频

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>

标签:function,vedio,m3u8,url,hls,js,video,播放,testVideo
来源: https://www.cnblogs.com/ooo51o/p/15540662.html