音声の録音とスクリーン キャプチャ

このガイドでは、chrome.tabCapturegetDisplayMedia() などの API を使用して、タブ、ウィンドウ、画面から音声や動画を録画するさまざまな方法について説明します。

画面の録画

画面を録画するには、getDisplayMedia() を呼び出します。これにより、以下に示すダイアログ ボックスがトリガーされます。これにより、ユーザーは共有するタブ、ウィンドウ、画面を選択でき、録画が行われていることが明確にわかります。

example.com の画面共有ダイアログ
example.com の画面共有ダイアログ

次の例では、音声と動画の両方を録画するためのアクセスをリクエストしています。

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

コンテンツ スクリプト内で呼び出された場合、ユーザーが新しいページに移動すると、録画は自動的に終了します。バックグラウンドで、またはナビゲーションをまたいで録画するには、DISPLAY_MEDIA の理由を指定してオフスクリーン ドキュメントを使用します。

ユーザーの操作に基づくタブのキャプチャ

getDisplayMedia() を呼び出すと、共有したい内容をユーザーに尋ねるダイアログがブラウザに表示されます。しかし、ユーザーがアクション ボタンをクリックして特定のタブの拡張機能を呼び出したので、このプロンプトを表示せずにすぐにタブのキャプチャを開始したい場合があります。

バックグラウンドで音声と動画を記録

Chrome 116 以降では、Service Worker で chrome.tabCapture API を呼び出して、ユーザー操作に従ってストリーム ID を取得できます。その後、これを画面外ドキュメントに渡して、録画を開始できます。

Service Worker で次の操作を行います。

chrome.action.onClicked.addListener(async (tab) => {
  const existingContexts = await chrome.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 chrome.offscreen.createDocument({
      url: 'offscreen.html',
      reasons: ['USER_MEDIA'],
      justification: 'Recording from chrome.tabCapture API',
    });
  }

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

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

次に、画面外ドキュメントで次の操作を行います。

chrome.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 recording the MediaStream.
  }
});

詳細な例については、タブキャプチャ - レコーダーのサンプルをご覧ください。

新しいタブで音声と動画を録画

Chrome 116 より前のバージョンでは、Service Worker で chrome.tabCapture API を使用したり、その API によって作成されたストリーム ID を画面外ドキュメントで使用したりすることはできませんでした。これらはいずれも、上記のアプローチの要件です。

代わりに、新しいタブまたはウィンドウで拡張機能ページを開いて、直接ストリームを取得できます。正しいタブを取得するように targetTabId プロパティを設定します。

まず、(ポップアップや Service Worker で)拡張機能のページを開きます。

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

拡張機能のページで次の操作を行います。

chrome.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);
});

または、画面録画アプローチの使用を検討してください。画面外のドキュメントを使用してバックグラウンドで録画を行う一方で、録画元のタブ、ウィンドウ、画面を選択するダイアログをユーザーに表示します。

ポップアップで音声を録音する

音声の録音のみが必要な場合は、拡張機能のポップアップで chrome.tabCapture.capture を使用して直接ストリームを取得できます。ポップアップを閉じると、録画は停止します。

chrome.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)
});

ナビゲーションをまたいで記録を保持する必要がある場合は、前のセクションで説明した方法を使用することを検討してください。

その他の考慮事項

ストリームの録画方法について詳しくは、MediaRecorder API をご覧ください。