MediaDeviceInfo.groupId - Web APIs 编辑

The groupId readonly property of the MediaDeviceInfo interface returns a DOMString that is a group identifier. Two devices have the same group identifier if they belong to the same physical device; for example, a monitor with both a built-in camera and microphone.

Syntax

var groupID = mediaDeviceInfo.groupId;

Value

A DOMString which uniquely identifies the group of related devices to which this device belongs.

Specifications

SpecificationStatusComment
Media Capture and Streams
The definition of 'groupId' in that specification.
Candidate RecommendationInitial definition.

Examples

In this example, we assemble a list of the devices which are part of the same group as a given device. This might be used to produce a user interface that gathers associated devices together for presentation purposes, or to make it easy for the user to choose to use the built-in camera and microphone on the same display at the same time.

const getDeviceGroup = mainDevInfo => {
  let devList = [];

  navigator.mediaDevices.enumerateDevices()
  .then(devices => {
    devices.forEach(device => {
      if (device.groupId === mainDevInfo.groupId) {
        devList.push(device);
      }
    });
  });

  return devList;
};

The getDeviceGroup() function takes as input the MediaDeviceInfo object describing the device for which a group list is to be built. The function starts by initializing the result array, devList, to be an empty array.

Then navigator.mediaDevices.enumerateDevices() is called to get the list of all media devices. Once the promise resolves, we walk the list using forEach(). For each device, if its groupId matches the main device's groupId, we push the MediaDeviceInfo object onto the list.

Finally, the list, which now contains a MediaDeviceInfo object for each device in the same group, is returned to the caller.

This could be altered easily to either leave out the passed-in device from the returned list, or to place it at the top of the list, by comparing the two objects' deviceId values, only pushing the device onto the result list if it doesn't match.

This version of the example puts the passed-in device at the top of the result list, then adds any other members of the group that are found:

const getDeviceGroup = mainDevInfo => {
  let devList = [mainDevInfo];

  navigator.mediaDevices.enumerateDevices()
  .then(devices => {
    devices.forEach(device => {
      if ((device.groupId === mainDevInfo.groupId) &&
          (device.deviceId !== mainDevInfo.deviceId)) {
        devList.push(device);
      }
    });
  });

  return devList;
};

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:143 次

字数:4346

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文