我正在尝试创建一个Chrome扩展程序,该扩展程序可以捕获 Activity 选项卡中的音频,然后将其发送到另一台服务器,或者使其可以通过URL进行访问。

我正在使用 chrome.tabCapture.capture API并可以成功获取选项卡音频的MediaStream,但是在此之后我不知道该怎么办。

Chrome文档与MediaStreams无关,因此我浏览了一些文档here并与JS调试器一起使用,以查看可用的方法,但找不到将MediaStream发送到某处的方法。

最佳答案

现在可以使用MediaRecorder在JS中本地记录流。有一个演示here,而w3c规范是here

演示中的startRecording方法要求将window.stream设置为MediaStream实例。

// The nested try blocks will be simplified when Chrome 47 moves to Stable
var mediaRecorder;
var recordedBlobs;
window.stream = myMediaStreamInstance;
function startRecording() {
  var options = {mimeType: 'video/webm', bitsPerSecond: 100000};
  recordedBlobs = [];
  try {
    mediaRecorder = new MediaRecorder(window.stream, options);
  } catch (e0) {
    console.log('Unable to create MediaRecorder with options Object: ', e0);
    try {
      options = {mimeType: 'video/webm,codecs=vp9', bitsPerSecond: 100000};
      mediaRecorder = new MediaRecorder(window.stream, options);
    } catch (e1) {
      console.log('Unable to create MediaRecorder with options Object: ', e1);
      try {
        options = 'video/vp8'; // Chrome 47
        mediaRecorder = new MediaRecorder(window.stream, options);
      } catch (e2) {
        alert('MediaRecorder is not supported by this browser.\n\n' +
            'Try Firefox 29 or later, or Chrome 47 or later, with Enable experimental Web Platform features enabled from chrome://flags.');
        console.error('Exception while creating MediaRecorder:', e2);
        return;
      }
    }
  }
  console.log('Created MediaRecorder', mediaRecorder, 'with options', options);

  // do UI cleanup here
  mediaRecorder.onstop = function() {/** stop */};
  mediaRecorder.ondataavailable = function() {/** data avail */};
  mediaRecorder.start(10); // collect 10ms of data
  console.log('MediaRecorder started', mediaRecorder);
}
  • https://www.w3.org/TR/mediastream-recording/
  • https://simpl.info/mediarecorder/
  • 09-15 13:16