index.html 1.9 KB
<!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>