The 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.
Some user agents subclass this interface to provide more precise information or functionality, like in CanvasCaptureMediaStream.
Constructor
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
MediaStreamTrackobjects).
Properties
This interface inherits properties from its parent, EventTarget.
MediaStream.activeRead only- A Boolean value that returns
trueif theMediaStreamis active, orfalseotherwise. MediaStream.endedRead only- A Boolean value set to
trueif the end of the stream has been reached. This has been removed from the specification; you should instead check the value ofMediaStreamTrack.readyStateto see if its value is"ended"for the track or tracks you want to ensure have finished playing.
MediaStream.idRead only- A
DOMStringcontaining 36 characters denoting a universally unique identifier (UUID) for the object.
Event handlers
MediaStream.onaddtrack- An
EventHandlercontaining the action to perform when anaddtrackevent is fired when a newMediaStreamTrackobject is added. MediaStream.onremovetrack- An
EventHandlercontaining the action to perform when aremovetrackevent is fired when aMediaStreamTrackobject is removed from it.
Methods
This interface inherits methods from its parent, EventTarget.
MediaStream.addTrack()- Stores a copy of the
MediaStreamTrackgiven as argument. If the track has already been added to theMediaStreamobject, nothing happens.
MediaStream.clone()- Returns a clone of the
MediaStreamobject. The clone will, however, have a unique value forid.
MediaStream.getAudioTracks()- Returns a list of the
MediaStreamTrackobjects stored in theMediaStreamobject that have theirkindattribute 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 returnsnull. If several tracks have the same ID, it returns the first one. MediaStream.getTracks()- Returns a list of all
MediaStreamTrackobjects stored in theMediaStreamobject, regardless of the value of thekindattribute. 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
MediaStreamTrackobjects stored in theMediaStreamobject that have theirkindattribute 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()- Removes the
MediaStreamTrackgiven as argument. If the track is not part of theMediaStreamobject, nothing happens.
Specifications
| Specification | Status | Comment |
|---|---|---|
| Media Capture and Streams The definition of 'MediaStream' in that specification. |
Editor's Draft |
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | No support | No support | ? |
getAudioTracks() and getVideoTracks() |
(Yes) | ? | 23.0 (23.0) | No support | No support | ? |
id |
(Yes) | ? | 41.0 (41.0) | No support | No support | ? |
label and stop() |
No support [1] | ? | No support | No support | ? | |
ended |
No support [2] | ? | No support | No support | ? | |
onaddtrack |
(Yes) | ? | 50 (50) | No support | No support | ? |
active |
? | ? | 52 (52) | No support | No support | ? |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | ? | (Yes) | (Yes) | (Yes) | No support | No support | ? | (Yes) |
getAudioTracks() and getVideoTracks() |
? | (Yes) | ? | 23.0 (23.0) | No support | No support | ? | (Yes) |
id |
? | (Yes) | ? | 41.0 (41.0) | No support | No support | ? | (Yes) |
label and stop() |
? | No support [1] | ? | No support | No support | ? | No support [1] | |
ended |
? | No support [2] | ? | No support | No support | ? | No support [2] | |
onaddtrack |
? | (Yes) | ? | 50.0 (50) | No support | No support | ? | (Yes) |
active |
? | ? | ? | 52.0 (52) | No support | No support | ? | ? |
[1] Deprecated in Chrome 45, removed in Chrome 54.
[2] Deprecated in Chrome 52.
See also
- Using the MediaStream API
- WebRTC API
- Web Audio API
MediaStreamTrack