当 mediaStreamTrack.enabled 被修改时,我如何收到 notification/event?

How can I receive a notification/event when mediaStreamTrack.enabled is modified?

我正在使用 getUserMedia 访问网络摄像头。我有一个功能可以打开和关闭视频,执行以下操作:

  var videoTracks = this.stream.getVideoTracks();
  if (videoTracks.length === 0) {
    trace('No local video available.');
    return;
  }

  trace('Toggling video mute state.');
  for (var i = 0; i < videoTracks.length; ++i) {
    videoTracks[i].enabled = !videoTracks[i].enabled;
  }

  trace('Video ' + (videoTracks[0].enabled ? 'unmuted.' : 'muted.'));

enabled 的值改变时如何接收事件?我尝试使用 Object.observe,但它不起作用。

据我所知,目前在 enabled 属性 更改时没有调用任何事件 fired/callback。

来自here

Also, there is no "onmuted" and "onunmuted" event defined or fired in the WebRTC native implementations.

您可能需要自己构建此机制:

Keep in mind that you're disabling a media track locally; it will not fire any event on target users side. If you disabled video track; then it will cause "blank-video" on target-users side.

You can manually fire events like "onmuted" or "onmediatrackdisabled" by using socket that was used for signaling. You can send/emit messages like:

yourSignalingSocket.send({
    isMediaStreamTrackDisabled: true,
    mediaStreamLabel: stream.label
});

根据 spec 这最终应该是 MediaStreamTrack 接口的一部分:

onmute of type EventHandler,

This event handler, of type mute, must be supported by all objects implementing the MediaStreamTrack interface.

我尝试在 Chrome (43) 中为轨道的 onmute 分配一个函数,但它从未被调用 (looks like this is not implemented yet)。