大网数据广告活动
Web 标准与前端开发

MediaDeviceInfo

· 52CE 我爱测

The **`MediaDeviceInfo`** interface of the Media Capture and Streams API contains information that describes a single media input or output device.

Media Capture and Streams

The MediaDeviceInfo interface of the Media Capture and Streams API contains information that describes a single media input or output device.

The list of devices obtained by calling MediaDevices.enumerateDevices is an array of MediaDeviceInfo objects, one per media device.

Instance properties

  • MediaDeviceInfo.deviceId
    • : Returns a string that is an identifier for the represented device that is persisted across sessions. It is un-guessable by other applications and unique to the origin of the calling application. It is reset when the user clears cookies (for Private Browsing, a different identifier is used that is not persisted across sessions).
  • MediaDeviceInfo.groupId
    • : Returns a string 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 a microphone.
  • MediaDeviceInfo.kind
    • : Returns an enumerated value that is either "videoinput", "audioinput" or "audiooutput".
  • MediaDeviceInfo.label
    • : Returns a string describing this device (for example "External USB Webcam").

[!NOTE] For security reasons, the label field is always blank unless an active media stream exists or the user has granted persistent permission for media device access. The set of device labels could otherwise be used as part of a fingerprinting mechanism to identify a user.

Instance methods

  • MediaDeviceInfo.toJSON()
    • : Returns a JSON-serializable plain object representing the MediaDeviceInfo object. Automatically called by JSON.stringify().

Example

Here's an example that uses MediaDevices.enumerateDevices to get a list of devices.

if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
  console.log("enumerateDevices() not supported.");
} else {
  // List cameras and microphones.
  navigator.mediaDevices
    .enumerateDevices()
    .then((devices) => {
      devices.forEach((device) => {
        console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
      });
    })
    .catch((err) => {
      console.log(`${err.name}: ${err.message}`);
    });
}

This might produce:

videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

or if one or more media streams are active, or if persistent permissions have been granted:

videoinput: FaceTime HD Camera (Built-in) id=csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: default (Built-in Microphone) id=RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: Built-in Microphone id=r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=

Specifications

Browser compatibility

See also

  • WebRTC API
  • MediaDevices.enumerateDevices
  • MediaDevices.getUserMedia

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。