《前端非主流知识》webRTC

105 阅读1分钟
<!DOCTYPE html>
<html>
<head>
  <title>WebRTC音视频录制演示</title>
</head>
<body>
  <h1>WebRTC音视频录制演示</h1>
  <!-- 用于展示录制视频流的视频元素 -->
  <video id="video" autoplay muted></video>
  <!-- 录制控制按钮 -->
  <button id="startButton">开始录制</button>
  <button id="stopButton">停止录制</button>
  <button id="downloadButton" disabled>下载录制文件</button>

  <script>
    // 获取页面中所需的DOM元素
    const videoElement = document.getElementById('video');
    const startButton = document.getElementById('startButton');
    const stopButton = document.getElementById('stopButton');
    const downloadButton = document.getElementById('downloadButton');

    // 定义录制相关的变量和数据
    let mediaRecorder;
    let recordedChunks = [];

    // 异步函数用于获取媒体设备(摄像头和麦克风)权限、获取视频流并展示在video元素上
    async function setupMedia() {
      try {
        // 请求摄像头和麦克风权限
        const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
        
        // 将获取的流设置为video元素的源
        videoElement.srcObject = stream;
        
        // 创建一个MediaRecorder实例来处理录制
        mediaRecorder = new MediaRecorder(stream);

        // 设置录制数据和停止事件的处理函数
        mediaRecorder.ondataavailable = handleDataAvailable;
        mediaRecorder.onstop = handleStop;
        
        // 启用"开始录制"按钮
        startButton.disabled = false;
      } catch (err) {
        console.error('访问媒体设备时出错:', err);
      }
    }

    // 开始录制的函数
    function startRecording() {
      // 清空之前录制的数据
      recordedChunks = [];
      
      // 启动MediaRecorder
      mediaRecorder.start();
      
      // 禁用"开始录制"按钮,启用"停止录制"按钮
      startButton.disabled = true;
      stopButton.disabled = false;
    }

    // 停止录制的函数
    function stopRecording() {
      // 停止MediaRecorder
      mediaRecorder.stop();
      
      // 启用"开始录制"按钮,禁用"停止录制"按钮
      startButton.disabled = false;
      stopButton.disabled = true;
    }

    // 处理录制的数据的函数
    function handleDataAvailable(event) {
      // 将录制的数据块推入recordedChunks数组中
      recordedChunks.push(event.data);
    }

    // 处理录制结束事件的函数
    function handleStop() {
      // 使用录制的数据块创建一个Blob对象,设置正确的MIME类型
      const blob = new Blob(recordedChunks, { type: 'video/webm' });
      
      // 创建一个Blob的URL
      const url = URL.createObjectURL(blob);
      
      // 配置下载按钮的属性,使其指向Blob的URL,并启用按钮
      downloadButton.href = url;
      downloadButton.download = 'recorded.webm';
      downloadButton.disabled = false;
    }

    // 初始化媒体设置
    setupMedia();

    // 添加按钮的事件监听器
    startButton.addEventListener('click', startRecording);
    stopButton.addEventListener('click', stopRecording);

    // 添加下载按钮的点击事件
    downloadButton.addEventListener('click', () => {
      // 检查是否有录制数据可供下载
      if (recordedChunks.length === 0) {
        alert('没有可用的录制数据。');
        return;
      }

      // 创建一个Blob对象并生成下载链接
      const blob = new Blob(recordedChunks, { type: 'video/webm' });
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'recorded.webm';
      a.click();
    });
  </script>
</body>
</html>