--- title: RTCPeerConnection.removeStream() slug: Web/API/RTCPeerConnection/removeStream translation_of: Web/API/RTCPeerConnection/removeStream ---

{{APIRef("WebRTC")}}{{SeeCompatTable}}

RTCPeerConnection.removeStream() 方法用来移除本地音频或视频的 {{domxref("媒体流")}} 。 如果已经发生交互,远程主机可能需要使用一个新的媒体流。

当 {{domxref("RTCPeerConnection.signalingState", "signalingState")}} 的值为"closed"时,将抛出InvalidStateError异常。当  {{domxref("RTCPeerConnection.signalingState", "signalingState")}} 值为"stable"时, 将触发{{domxref("RTCPeerConnection")}}的 {{event("negotiationneeded")}} 事件。

语法

pc.removeStream(mediaStream);

此方法没有返回值。

参数

mediaStream
是 {{domxref("MediaStream")}} 类型的表示要移除的媒体流

例子

var pc, videoStream;
navigator.getUserMedia({video: true}, function(stream) {
  pc = new RTCPeerConnection();
  videoStream = stream;
  pc.addStream(stream);
}
document.getElementById("closeButton").addEventListener("click", function(event) {
  pc.removeStream(videoStream);
  pc.close();
}, false);

规范

Specification Status Comment
{{ SpecName('WebRTC 1.0', '#widl-RTCPeerConnection-removeStream-void-MediaStream-stream', 'RTCPeerConnection.removeStream()') }} {{ Spec2('WebRTC 1.0') }} Initial specification.

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} [1] {{ CompatVersionUnknown }} [1] {{ CompatNo() }} {{ CompatVersionUnknown() }} {{ CompatUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

[1] Though this property is not prefixed, the interface it belongs to is.

扩展阅读