RTCRtpSender 方法 setStreams() associates the sender's track 采用指定 MediaStream or array of MediaStream 对象。

句法

rtcRtpSender.setStreams(mediaStream);
rtcRtpSender.setStreams([mediaStream...]);
					

参数

mediaStream or [mediaStream...] 可选
MediaStream object—or an array of multiple MediaStream objects—identifying the streams to which the RTCRtpSender 's track belongs. If this parameter isn't specified, no new streams will be associated with the track.

返回值

None.

异常

InvalidStateError

The sender's connection is closed.

描述

setStreams() is purely additive. It doesn't remove the track from any streams; it simply adds it to new ones. If you specify streams to which the track already belongs, that stream is unaffected.

Once the track has been added to all of the streams, renegotiation of the connection will be triggered by the negotiationneeded event being dispatched to the RTCPeerConnection to which the sender belongs.

范例

This example adds all of an RTCPeerConnection 's tracks to the specified stream.

function addTracksToStream(stream) {
  let senders = pc.getSenders();
  senders.forEach((sender) => {
    if (sender.track && (sender.transport.state === connected)) {
      sender.setStreams(stream);
    }
  });
}
					

After calling the RTCPeerConnection 方法 getSenders() to get the list of the connection's senders, the addTracksToStream() function iterates over the list. For each sender, if the sender's track is non-null and its transport's state is connected , we call setStreams() to add the track to the stream 指定。

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCRtpSender.setStreams()' in that specification.
候选推荐

浏览器兼容性

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.

No compatibility data found. Please contribute data for "api.RTCRtpSender.setStreams" (depth: 1) to the MDN 兼容性数据存储库 .

元数据

  • 最后修改: