MediaDeviceInfo
MediaDeviceInfo 介面是 Media Capture and Streams API 的一部分,它包含描述單個媒體輸入或輸出裝置的資訊。
透過呼叫 navigator.mediaDevices.enumerateDevices() 獲取的裝置列表是 MediaDeviceInfo 物件的陣列,每個物件代表一個媒體裝置。
例項屬性
MediaDeviceInfo.deviceId只讀-
返回一個字串,該字串是表示裝置並在會話之間持久存在的識別符號。此識別符號無法被其他應用程式猜測,並且對於呼叫應用程式的源是唯一的。當用戶清除 Cookie 時,它會被重置(對於隱私瀏覽,會使用一個不同的識別符號,該識別符號不會在會話之間持久存在)。
MediaDeviceInfo.groupId只讀-
返回一個字串,該字串是組識別符號。如果兩個裝置屬於同一個物理裝置,則它們具有相同的組識別符號——例如,一個帶有內建攝像頭和麥克風的顯示器。
MediaDeviceInfo.kind只讀-
返回一個列舉值,該值可以是
"videoinput"(影片輸入)、"audioinput"(音訊輸入)或"audiooutput"(音訊輸出)。 MediaDeviceInfo.label只讀-
返回一個描述該裝置的字串(例如“外部 USB 網路攝像頭”)。
注意: 出於安全原因,label 欄位始終為空,除非存在活動的媒體流或使用者已授予媒體裝置訪問的持久許可權。否則,裝置標籤集可能被用作 指紋識別 機制的一部分來識別使用者。
例項方法
MediaDeviceInfo.toJSON()-
返回
MediaDeviceInfo物件的 JSON 表示形式。
示例
這是一個使用 enumerateDevices() 獲取裝置列表的示例。
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.kind}: ${device.label} id = ${device.deviceId}`);
});
})
.catch((err) => {
console.log(`${err.name}: ${err.message}`);
});
}
這可能會產生
bash
videoinput: id = csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: id = RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: id = r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=
或者,如果一個或多個媒體流處於活動狀態,或者已獲得持久許可權
bash
videoinput: FaceTime HD Camera (Built-in) id=csO9c0YpAf274OuCPUA53CNE0YHlIr2yXCi+SqfBZZ8=
audioinput: default (Built-in Microphone) id=RKxXByjnabbADGQNNZqLVLdmXlS0YkETYCIbg+XxnvM=
audioinput: Built-in Microphone id=r2/xw1xUPIyZunfV1lGrKOma5wTOvCkWfZ368XCndm0=
規範
| 規範 |
|---|
| 媒體捕獲和流 # device-info |
瀏覽器相容性
載入中…