1

我有MediaStream1 个音轨和 2 个视频轨。此流是通过将来自的音频和视频轨道与来自navigator.mediaDevices.getUserMedia的视频轨道相结合而创建的navigator.mediaDevices.getDisplayMedia

var camStream = await navigator.mediaDevices.getUserMedia({audio: true, video: true, width: "1280"});
var screenStream = await navigator.mediaDevices.getDisplayMedia({video: true, width: "1280"});
var screenTrack = screenStream.getVideoTracks()[0];

camStream.addTrack(screenTrack);

此时,camStream有 1 个音轨和 2 个视频轨。

我有一个 HTML 元素<video id="local-video"></video>,通过设置 srcObject 属性来播放流:

document.getElementById("local-video").srcObject = camStream;

问题是我无法选择正在播放的曲目。camStream 视频轨道总是在 screenStream 视频轨道上播放。有没有一种方法可以让我将两个视频轨道都保留在流中,但选择视频元素中当前正在播放的轨道?

到目前为止,唯一可行的方法是从流中删除一个视频轨道,以便另一个可以播放,但我想将两个视频轨道都保留在流中,以便我可以黑白切换它们。

4

1 回答 1

2

MediaElement.videoTracksand应该是解决这个MediaElement.audioTracks问题的通用解决方案,但是到目前为止,浏览器的支持仍然不是很好,因为没有浏览器真正正式支持它,而且根本没有 MediaStreams 支持它......

所以下面是一个片段,展示了它应该是怎样的,但那是为了未来。

const vid = document.querySelector("video");
const btn = document.querySelector("button");
const track1 = makeVideoTrack("red");
const track2 = makeVideoTrack("blue");

const stream = new MediaStream([track1, track2]);
vid.srcObject = stream;

btn.onclick = () => {
  // setting one track to selected will unselect the previously selected one
  // so we need to grab the current state to switch it
  const state = vid.videoTracks[0].selected;
  vid.videoTracks[1].selected = state;
  vid.videoTracks[0].selected = !state;
};

function makeVideoTrack(color) {
  const canvas = document.createElement("canvas");
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = color;
  let x = 0;
  const anim = () => {
    x = (x + 1) % canvas.width;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillRect(x-5, 0, 10, canvas.height);
    requestAnimationFrame( anim );
  };
  anim();
  return canvas.captureStream().getVideoTracks()[0];
}
<video controls autoplay muted></video>

<button>switch video track</button>

今天,在您可以访问 MediaStream的情况下videoTrack,可以做的是构建一个仅包含您想要的新 MediaStream。(人们可能很想对这些曲目进行重新排序,但这仅适用于当前的 Chrome 和 Firefox,并且规范清楚地表明我们不应该依赖这种行为)

const vid = document.querySelector("video");
const btn = document.querySelector("button");
const tracks = [makeVideoTrack("red"), makeVideoTrack("blue")];
let selected_index = 0;
vid.srcObject = new MediaStream([ tracks[selected_index] ]);

btn.onclick = () => {
  selected_index = (selected_index + 1) % tracks.length
  vid.srcObject = new MediaStream([ tracks[selected_index] ]);
};

function makeVideoTrack(color) {
  const canvas = document.createElement("canvas");
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = color;
  let x = 0;
  const anim = () => {
    x = (x + 1) % canvas.width;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillRect(x-5, 0, 10, canvas.height);
    requestAnimationFrame( anim );
  };
  anim();
  return canvas.captureStream().getVideoTracks()[0];
}
<video controls autoplay muted></video>

<button>switch video track</button>

于 2020-12-21T01:57:14.583 回答