數 getDisplayMedia()(屬于 navigator.mediaDevices 的一部分)與 getUserMedia() 類似,用于打開顯示內容(或部分內容,如窗口)。返回的 MediaStream 與使用 getUserMedia() 時相同。
顯示鼠標與否
getDisplayMedia() 的約束條件與常規視頻或音頻輸入資源的限制不同。
{video: {cursor: 'always' | 'motion' | 'never',displaySurface: 'application' | 'browser' | 'monitor' | 'window'}
}
上述代碼片段展示了屏幕錄制的特殊限制的工作原理。請注意,并非所有支持顯示媒體支持的瀏覽器都支持這些屬性。
回聲消除:
async function getConnectedDevices(type) {const devices = await navigator.mediaDevices.enumerateDevices();return devices.filter(device => device.kind === type)
}// Open camera with at least minWidth and minHeight capabilities
async function openCamera(cameraId, minWidth, minHeight) {const constraints = {'audio': {'echoCancellation': true},'video': {'deviceId': cameraId,'width': {'min': minWidth},'height': {'min': minHeight}}}return await navigator.mediaDevices.getUserMedia(constraints);
}const cameras = getConnectedDevices('videoinput');
if (cameras && cameras.length > 0) {// Open first available video camera with a resolution of 1280x720 pixelsconst stream = openCamera(cameras[0].deviceId, 1280, 720);
}
'audio': {'echoCancellation': true},
echoCancellation
A ConstrainBoolean object specifying whether or not echo cancellation is preferred and/or required.
latency 延遲
A ConstrainDouble specifying the latency or range of latencies which are acceptable and/or required.
noiseSuppression 噪音消除
A ConstrainBoolean which specifies whether noise suppression is preferred and/or required.
https://webrtc.org/getting-started/media-devices?hl=zh-cn
應用約束
為了確定某個媒體流的特定軌道的實際配置,我們可以調用 MediaStreamTrack.getSettings(),它會返回當前應用的 MediaTrackSettings。
此外,也可以通過對媒體軌道上調用 applyConstraints() 來更新已打開的媒體設備上的軌道約束條件。這樣,應用無需重新關閉現有音頻流,即可重新配置媒體設備。
一鍵遠程控制電腦手機,kkview.com