W3cubDocs

/Web APIs

MediaStream

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.

You can obtain a MediaStream object either by using the constructor or by calling functions such as MediaDevices.getUserMedia(), MediaDevices.getDisplayMedia(), or HTMLCanvasElement.captureStream().

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

EventTarget MediaStream

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 MediaStreamTrack objects).

Instance properties

This interface inherits properties from its parent, EventTarget.

MediaStream.active Read only

A Boolean value that returns true if the MediaStream is active, or false otherwise.

MediaStream.id Read only

A string containing a 36-character universally unique identifier (UUID) for the object.

Instance methods

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()

Removes the 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.

removetrack

Fired when a MediaStreamTrack object has been removed.

active

Fired when the MediaStream is activated.

inactive

Fired when the MediaStream is inactivated.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
MediaStream 5521 7912 44 No 4215 11 554.4 5525 42 4214 11 6.01.5
MediaStream 5521 7912 15 No 4215 11 554.4 5525 15 4214 11 6.01.5
active 42 12 52 No 29 11 42 42 52 29 11 4.0
active_event 42 12 No No 29 No 42 42 No 29 No 4.0
addTrack 26 12 44 No No 11 37 26 No No 11 1.5
addtrack_event 26 12 50 No No 11 4.4 26 50 No 11 1.5
clone 35 12 48 No 22 11 37 35 48 22 11 3.0
getAudioTracks 26 12
22Before Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No 15 11 37 26
22Before Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No 11 1.5
getTrackById 26 12 49 No No 11 37 26 49 No 11 1.5
getTracks 38 12 34 No 25 11 38 38 34 25 11 3.0
getVideoTracks 26 12
22Before Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No 15 11 37 26
22Before Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
No 11 1.5
id 26 12 41 No 15 11 4.4 26 41 14 11 1.5
inactive_event 42 12 No No 29 No 42 42 No 29 No 4.0
removeTrack 26 12 44 No 15 11 37 26 44 No 11 1.5
removetrack_event 26 12 72
59–72Before Firefox 72, the event handler was exposed but had no effect.
No No 11 4.4 26 79
59–79Before Firefox 72, the event handler was exposed but had no effect.
No 11 1.5

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream