index.html
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<title>多路摄像头实时播放</title>
<script src="https://cdn.jsdelivr.net/npm/zlmrtc4player"></script>
<style>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
gap: 10px;
}
video {
width: 100%;
height: auto;
background: black;
}
</style>
</head>
<body>
<h2>摄像头分屏实时播放</h2>
<div class="grid" id="videoGrid"></div>
<script>
const videoGrid = document.getElementById("videoGrid");
function playStream(videoEl, app, stream, retryCount = 0) {
const maxRetries = 5;
const retryDelay = 3000;
const player = new ZLMRTCClient.Endpoint({
video: videoEl,
protocol: location.protocol === "https:" ? "wss" : "ws",
host: location.hostname,
port: 80,
app,
stream
});
player.play().catch(() => {
if (retryCount < maxRetries) {
console.warn(`播放失败,重试第 ${retryCount + 1} 次:${stream}`);
setTimeout(() => playStream(videoEl, app, stream, retryCount + 1), retryDelay);
} else {
console.error(`播放失败:${stream},已达最大重试次数`);
}
});
}
cameras = [
{
name: '摄像头1',
app: 'live',
stream: 'cam1'
}
];
cameras => {
cameras.forEach(cam => {
const container = document.createElement('div');
const label = document.createElement('p');
label.textContent = cam.name;
const video = document.createElement('video');
video.autoplay = true;
video.muted = true;
video.playsInline = true;
video.controls = true;
container.appendChild(label);
container.appendChild(video);
videoGrid.appendChild(container);
});
}
</script>
</body>
</html>