MediaStream

MediaStream interface represents a stream of media content. A stream consists of several tracks such as video or audio tracks. Each track is specified as an instance of MediaStreamTrack . You can obtain a MediaStream object either by using the constructor or by calling MediaDevices.getUserMedia() .

Some user agents subclass this interface to provide more precise information or functionality, like in CanvasCaptureMediaStream .

构造函数

MediaStream()
Creates and returns a new MediaStream object. You can create an empty stream, a stream which is based upon an existing stream, or a stream that contains a specified list of tracks (specified as an array of MediaStreamTrack objects).

特性

This interface inherits properties from its parent, EventTarget .

MediaStream.active 只读
A Boolean value that returns true MediaStream is active, or false 否则。
MediaStream.ended 只读
A Boolean value set to true if the end of the stream has been reached. This has been removed from the specification; you should instead check the value of MediaStreamTrack.readyState to see if its value is ended for the track or tracks you want to ensure have finished playing.
MediaStream.id 只读
A DOMString containing 36 characters denoting a universally unique identifier (UUID) for the object.

事件处理程序

MediaStream.onaddtrack
An EventHandler containing the action to perform when an addtrack event is fired when a new MediaStreamTrack object is added.
MediaStream.onremovetrack
An EventHandler containing the action to perform when a removetrack event is fired when a MediaStreamTrack object is removed from it.

方法

This interface inherits methods from its parent, EventTarget .

MediaStream.addTrack()
Stores a copy of the MediaStreamTrack given as argument. If the track has already been added to the MediaStream object, nothing happens.
MediaStream.clone()
Returns a clone of the MediaStream object. The clone will, however, have a unique value for id .
MediaStream.getAudioTracks()
Returns a list of the MediaStreamTrack objects stored in the MediaStream object that have their kind attribute set to audio . The order is not defined, and may not only vary from one browser to another, but also from one call to another.
MediaStream.getTrackById()
Returns the track whose ID corresponds to the one given in parameters, trackid . If no parameter is given, or if no track with that ID does exist, it returns null . If several tracks have the same ID, it returns the first one.
MediaStream.getTracks()
Returns a list of all MediaStreamTrack objects stored in the MediaStream object, regardless of the value of the kind attribute. The order is not defined, and may not only vary from one browser to another, but also from one call to another.
MediaStream.getVideoTracks()
Returns a list of the MediaStreamTrack objects stored in the MediaStream object that have their kind attribute set to "video" . The order is not defined, and may not only vary from one browser to another, but also from one call to another.
MediaStream.removeTrack()
移除 MediaStreamTrack given as argument. If the track is not part of the MediaStream object, nothing happens.

Events

addtrack
Fired when a new MediaStreamTrack object is added.
Also available via the onaddtrack 特性。
removetrack
Fired when a MediaStreamTrack object has been removed.
Also available via the onremovetrack 特性。
active
Fired when the MediaStream is activated.
inactive
Fired when the MediaStream is inactivated.

规范

规范 状态 Comment
媒体捕获和流
The definition of 'MediaStream' in that specification.
候选推荐

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MediaStream Chrome 55
55
21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 15 IE No Opera 42
42
15
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 11 WebView Android 55
55
≤37
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android 55
55
25
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 15 Opera Android 42
42
14
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 11 Samsung Internet Android 6.0
6.0
1.5
Prefixed
Prefixed Implemented with the vendor prefix: webkit
MediaStream() 构造函数 Chrome 21 Edge ≤18 Firefox 44 IE No Opera 42 Safari No WebView Android 37 Chrome Android 25 Firefox Android 42 Opera Android No Safari iOS No Samsung Internet Android 6.0
active Chrome 45 Edge 12 Firefox 52 IE No Opera No Safari Yes WebView Android 45 Chrome Android 45 Firefox Android 52 Opera Android No Safari iOS Yes Samsung Internet Android 5.0
active event Chrome 45 Edge ≤79 Firefox ? IE No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android 5.0
addTrack Chrome 26 Edge 12 Firefox 44 IE No Opera No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android 1.5
addtrack event Chrome Yes Edge 12 Firefox 50 IE No Opera No Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android No Safari iOS Yes Samsung Internet Android Yes
clone Chrome 45 Edge 12 Firefox 48 IE No Opera No Safari Yes WebView Android 45 Chrome Android 45 Firefox Android 48 Opera Android No Safari iOS Yes Samsung Internet Android 5.0
ended
弃用
Chrome ? — 54
不支持 ? — 54
Deprecated in Chrome 52.
Edge ? Firefox No IE No Opera ? — 39 Safari ? WebView Android ? — 54
不支持 ? — 54
Deprecated in Chrome 52.
Chrome Android ? — 54
不支持 ? — 54
Deprecated in Chrome 52.
Firefox Android No Opera Android ? — 41 Safari iOS ? Samsung Internet Android ? — 6.0
不支持 ? — 6.0
Deprecated in Samsung Internet 6.0.
getAudioTracks Chrome 26 Edge 12 Firefox 22
22
Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
IE No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 22
22
Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
Opera Android No Safari iOS Yes Samsung Internet Android 1.5
getTrackById Chrome 26 Edge 12 Firefox 49 IE No Opera No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 49 Opera Android No Safari iOS Yes Samsung Internet Android 1.5
getTracks
Chrome 45 Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android 45 Chrome Android 45 Firefox Android Yes Opera Android No Safari iOS Yes Samsung Internet Android 5.0
getVideoTracks
Chrome 26 Edge 12 Firefox 22
22
Prior to Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
IE No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 22
22
Prior to Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
Opera Android No Safari iOS Yes Samsung Internet Android 1.5
id Chrome ? — 54 Edge 12 — 79 Firefox 41 IE No Opera ? — 39 Safari Yes WebView Android ? — 54 Chrome Android ? — 54 Firefox Android 41 Opera Android ? — 41 Safari iOS Yes Samsung Internet Android ? — 6.0
inactive event Chrome 45 Edge ≤79 Firefox ? IE No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android 5.0
label
弃用
Chrome ? — 54
不支持 ? — 54
Deprecated in Chrome 45.
Edge ? Firefox ? IE No Opera No Safari ? WebView Android ? — 54
不支持 ? — 54
Deprecated in Chrome 45.
Chrome Android ? — 54
不支持 ? — 54
Deprecated in Chrome 45.
Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android ? — 6.0
不支持 ? — 6.0
Deprecated in Samsung Internet 5.0.
onactive Chrome 45 Edge ≤79 Firefox ? IE No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android 5.0
onaddtrack Chrome 26 Edge 12 Firefox 50 IE No Opera No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 50 Opera Android No Safari iOS Yes Samsung Internet Android 1.5
oninactive Chrome 45 Edge ≤79 Firefox ? IE No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android 5.0
onremovetrack Chrome 26 Edge 12 Firefox No IE No Opera No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android 1.5
removeTrack Chrome 26 Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android Yes Opera Android No Safari iOS Yes Samsung Internet Android 1.5
removetrack event Chrome Yes Edge 12 Firefox No IE No Opera No Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android Yes
stop
弃用
Chrome ? — 47 Edge 13 — 79 Firefox ? IE No Opera No Safari ? WebView Android ? — 47 Chrome Android ? — 47 Firefox Android ? Opera Android No Safari iOS ? Samsung Internet Android ? — 5.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅