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.

句法

var groupID = mediaDeviceInfo.groupId;
					

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

规范

规范 状态 注释
媒体捕获和流
The definition of 'groupId' in that specification.
候选推荐 初始定义。

范例

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;
};
					

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;
};
					

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
groupId Chrome 55 注意事项
55 注意事项
For earlier versions, this interface is available through the adapter.js polyfill
Edge 12 Firefox 39 注意事项
39 注意事项
Prior to Firefox 67, related devices are not actually grouped together by groupId .
IE 不支持 No Opera 不支持 No 注意事项
No 注意事项
This property can be used in Opera by using the adapter.js polyfill.
Safari 不支持 No WebView Android 55 注意事项
55 注意事项
For earlier versions, this interface is available through the adapter.js polyfill
Chrome Android 55 注意事项
55 注意事项
For earlier versions, this interface is available through the adapter.js polyfill
Firefox Android 39 注意事项
39 注意事项
Prior to Firefox 67, related devices are not actually grouped together by groupId .
Opera Android 不支持 No 注意事项
No 注意事项
This property can be used in Opera by using the adapter.js polyfill.
Safari iOS 不支持 No Samsung Internet Android 6.0 注意事项
6.0 注意事项
For earlier versions, this interface is available through the adapter.js polyfill

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

元数据

  • 最后修改:
  1. MediaDeviceInfo
  2. 特性
    1. deviceId
    2. groupId
    3. kind
    4. label

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1