这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

过时
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

RTCPeerConnection.getStreamById() 方法返回 MediaStream with the given id that is associated with local or remote end of the connection. If no stream matches, it returns null .

This property has been replaced with the RTCPeerConnection.getLocalStreams and RTCPeerConnection.getRemoteStreams properties. If you have code that uses stream , you will need to update, since browsers have begun to remove support for stream .

句法

var mediaStream = pc.getStream(id);

					

参数

id
DOMString corresponding to the stream to return.

范例

var stream = pc.getStreamById(myTrackId);
if (stream) {
  console.log("Found stream: " + stream.id);
}
					

Polyfill

Running the following code before any other code will create RTCPeerConnection.prototype.getStreamById() if it's not natively available.

// from: https://bugs.chromium.org/p/chromium/issues/detail?id=698163&desc=5#c10
RTCPeerConnection.prototype.getStreamById = function(id) {
  try {
    var localStreams = this.getLocalStreams();
    var remoteStreams = this.getRemoteStreams();
    var i;
    for (i = 0; i < localStreams.length; i++) {
     if (localStreams[i].id == id)
       return localStreams[i];
    }
    for (i = 0; i < remoteStreams.length; i++) {
     if (remoteStreams[i].id == id)
       return remoteStreams[i];
    }
  } catch(e) {}
  return null;
}
					

浏览器兼容性

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
getStreamById 弃用 非标 Chrome 56 — 62 Edge 15 — 79 Firefox 22 — 53 IE No Opera 43
43
Promise-based version.
不支持 37 — 43
Safari ? WebView Android 56 — 62 Chrome Android 56 — 62 Firefox Android 44 Opera Android 43
43
Promise-based version.
不支持 37 — 43
Safari iOS ? Samsung Internet Android 6.0 — 8.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

元数据

  • 最后修改: