การบันทึกเสียงและการจับภาพหน้าจอ

คู่มือนี้อธิบายแนวทางต่างๆ ในการบันทึกเสียงและวิดีโอจากแท็บ หน้าต่าง หรือ หน้าจอโดยใช้ API เช่น browser.tabCapture หรือ getDisplayMedia()

การบันทึกหน้าจอ

สำหรับการบันทึกหน้าจอ ให้เรียกใช้ getDisplayMedia() ซึ่งจะทริกเกอร์กล่องโต้ตอบ ที่แสดงด้านล่าง ซึ่งช่วยให้ผู้ใช้เลือกแท็บ หน้าต่าง หรือหน้าจอที่ต้องการแชร์ได้ และยังระบุอย่างชัดเจนว่ากำลังมีการบันทึก

กล่องโต้ตอบการแชร์หน้าจอสำหรับ example.com
กล่องโต้ตอบการแชร์หน้าจอสำหรับ example.com

ตัวอย่างต่อไปนี้ขอสิทธิ์เข้าถึงเพื่อบันทึกทั้งเสียงและวิดีโอ

const stream = await navigator.mediaDevices.getDisplayMedia({ audio: true, video: true });

หากเรียกใช้ภายใน Content Script การบันทึกจะสิ้นสุดโดยอัตโนมัติเมื่อผู้ใช้ไปยังหน้าใหม่ หากต้องการบันทึกในเบื้องหลังและในการไปยังส่วนต่างๆ ให้ใช้เอกสารนอกหน้าจอที่มีเหตุผลเป็น DISPLAY_MEDIA

การจับภาพแท็บตามท่าทางผู้ใช้

การเรียกใช้ getDisplayMedia() จะทำให้เบราว์เซอร์แสดงกล่องโต้ตอบที่ถามผู้ใช้ว่าต้องการแชร์อะไร อย่างไรก็ตาม ในบางกรณี ผู้ใช้เพิ่งคลิกปุ่มการดำเนินการเพื่อเรียกใช้ส่วนขยายสำหรับแท็บหนึ่งๆ และคุณต้องการเริ่มบันทึกแท็บทันทีโดยไม่ต้องแสดงข้อความแจ้งนี้

บันทึกเสียงและวิดีโอขณะล็อกหน้าจอหรือขณะใช้แอปอื่น

ตั้งแต่ Chrome 116 เป็นต้นไป คุณจะเรียกใช้ API browser.tabCapture ใน Service Worker เพื่อรับรหัสสตรีมหลังจากท่าทางผู้ใช้ได้ จากนั้นจะส่งไปยังเอกสารนอกหน้าจอเพื่อเริ่มบันทึก

ใน Service Worker

browser.action.onClicked.addListener(async (tab) => {
  const existingContexts = await browser.runtime.getContexts({});

  const offscreenDocument = existingContexts.find(
    (c) => c.contextType === 'OFFSCREEN_DOCUMENT'
  );

  // If an offscreen document is not already open, create one.
  if (!offscreenDocument) {
    // Create an offscreen document.
    await browser.offscreen.createDocument({
      url: 'offscreen.html',
      reasons: ['USER_MEDIA'],
      justification: 'Recording from browser.tabCapture API',
    });
  }

  // Get a MediaStream for the active tab.
  const streamId = await browser.tabCapture.getMediaStreamId({
    targetTabId: tab.id
  });

  // Send the stream ID to the offscreen document to start recording.
  browser.runtime.sendMessage({
    type: 'start-recording',
    target: 'offscreen',
    data: streamId
  });
});

จากนั้นในเอกสารที่อยู่นอกหน้าจอ ให้ทำดังนี้

browser.runtime.onMessage.addListener(async (message) => {
  if (message.target !== 'offscreen') return;
  
  if (message.type === 'start-recording') {
    const media = await navigator.mediaDevices.getUserMedia({
      audio: {
        mandatory: {
          chromeMediaSource: "tab",
          chromeMediaSourceId: message.data,
        },
      },
      video: {
        mandatory: {
          chromeMediaSource: "tab",
          chromeMediaSourceId: message.data,
        },
      },
    });

    // Continue to play the captured audio to the user.
    const output = new AudioContext();
    const source = output.createMediaStreamSource(media);
    source.connect(output.destination);

    // TODO: Do something to record the MediaStream.
  }
});

ดูตัวอย่างที่สมบูรณ์ได้ในตัวอย่าง Tab Capture - Recorder

บันทึกเสียงและวิดีโอในแท็บใหม่

ก่อน Chrome 116 จะไม่สามารถใช้ API browser.tabCapture ใน Service Worker หรือใช้รหัสสตรีมที่สร้างโดย API นั้นในเอกสารนอกหน้าจอได้ ทั้ง 2 อย่างนี้ เป็นข้อกำหนดสำหรับแนวทางข้างต้น

แต่คุณสามารถเปิดหน้าส่วนขยายในแท็บหรือหน้าต่างใหม่ และรับสตรีมได้โดยตรง ตั้งค่าพร็อพเพอร์ตี้ targetTabId เพื่อบันทึกแท็บที่ถูกต้อง

เริ่มต้นด้วยการเปิดหน้าส่วนขยาย (อาจอยู่ในป๊อปอัปหรือ Service Worker)

browser.windows.create({ url: browser.runtime.getURL("recorder.html") });

จากนั้นในหน้าส่วนขยาย ให้ทำดังนี้

browser.tabCapture.getMediaStreamId({ targetTabId: tabId }, async (id) => {
  const media = await navigator.mediaDevices.getUserMedia({
    audio: {
      mandatory: {
        chromeMediaSource: "tab",
        chromeMediaSourceId: id,
      },
    },
    video: {
      mandatory: {
        chromeMediaSource: "tab",
        chromeMediaSourceId: id,
      },
    },
  });

  // Continue to play the captured audio to the user.
  const output = new AudioContext();
  const source = output.createMediaStreamSource(media);
  source.connect(output.destination);
});

หรือจะใช้แนวทางการบันทึกหน้าจอก็ได้ ซึ่งจะช่วยให้คุณ บันทึกในเบื้องหลังโดยใช้เอกสารนอกหน้าจอ แต่จะแสดงกล่องโต้ตอบให้ผู้ใช้เลือกแท็บ หน้าต่าง หรือหน้าจอที่จะบันทึก

บันทึกเสียงในป๊อปอัป

หากต้องการบันทึกเฉพาะเสียง คุณสามารถรับสตรีมได้โดยตรงในป๊อปอัปของส่วนขยายโดยใช้ browser.tabCapture.capture เมื่อป๊อปอัปปิดลง ระบบจะหยุดการบันทึก

browser.tabCapture.capture({ audio: true }, (stream) => {
  // Continue to play the captured audio to the user.
  const output = new AudioContext();
  const source = output.createMediaStreamSource(stream);
  source.connect(output.destination);

  // TODO: Do something with the stream (e.g record it)
});

หากต้องการให้การบันทึกยังคงอยู่เมื่อมีการไปยังส่วนต่างๆ ให้ลองใช้วิธีที่อธิบายไว้ในส่วนก่อนหน้า

ข้อควรพิจารณาอื่นๆ

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีบันทึกสตรีมได้ที่ API ของ MediaRecorder