MediaDeviceInfo: phương thức toJSON()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2018.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Phương thức toJSON() của giao diện MediaDeviceInfo là một bộ tuần tự hóa; nó trả về biểu diễn JSON của đối tượng MediaDeviceInfo.
Cú pháp
js
toJSON()
Tham số
Không có.
Giá trị trả về
Một đối tượng JSON là sự tuần tự hóa của đối tượng MediaDeviceInfo.
Ví dụ
js
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.toJSON());
});
})
.catch((err) => {
console.log(`${err.name}: ${err.message}`);
});
}
Kết quả có thể là:
bash
Object { deviceId: "HJtTemQTM64Bivxv3ZEyKjCi1VR8042lPNpmXKObKJE=", kind: "videoinput", label: "", groupId: "Okm2l1YZTrwy8awTxE8QSLNFoVMdIXx++wLh68tbmv0=" }
Object { deviceId: "EqDubLxPlPeW+5w/ereWTF/3EaAMVHh9QBBHkZHiP0k=", kind: "audioinput", label: "", groupId: "Okm2l1YZTrwy8awTxE8QSLNFoVMdIXx++wLh68tbmv0=" }
Object { deviceId: "CanWttL2RnHOiS7FzzYXMIvLqVFE5S06Lfy8H//nhEw=", kind: "audioinput", label: "", groupId: "nOdLNeXGIw9oL9f2wH69SssQpRVs7cmt9jqZrUWgQwI=" }
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Media Capture and Streams> # dom-mediadeviceinfo-tojson> |