{{APIRef("Media Capture and Streams")}}
The MediaStream
interface represents a stream of media content. A stream consists of several tracks such as video or audio tracks.
Some user agents subclass this interface to provide more precise information or functionality, like in {{domxref("CanvasCaptureMediaStream")}}.
Properties
This interface inherits properties from its parent, {{domxref("EventTarget")}}.
- {{domxref("MediaStream.active")}} {{readonlyinline}}
- A Boolean value that returns
true
if theMediaStream
is active, orfalse
otherwise. - {{domxref("MediaStream.ended")}} {{readonlyInline}} {{obsolete_inline()}}
- A Boolean value set to
true
if the {{event("ended (MediaStream)", "ended")}} event has been fired on the object, meaning that the stream has been completely read, orfalse
if the end of the stream has not been reached. This has been removed from the specification; you should instead check the value of {{domxref("MediaStreamTrack.readyState")}} to see if it's"ended"
. For the track or tracks you want to check up on
- {{domxref("MediaStream.id")}} {{readonlyInline}}
- A {{domxref("DOMString")}} containing 36 characters denoting a universally unique identifier (UUID) for the object.
Event handlers
- {{domxref("MediaStream.onaddtrack")}}
- An {{domxref("EventHandler")}} containing the action to perform when an {{event("addtrack")}} event is fired when a new {{domxref("MediaStreamTrack")}} object is added.
- {{domxref("MediaStream.onremovetrack")}}
- An {{domxref("EventHandler")}} containing the action to perform when an {{event("removetrack")}} event is fired when a {{domxref("MediaStreamTrack")}} object is removed from it.
Constructor
- {{domxref("MediaStream.MediaStream", "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 {{domxref("MediaStreamTrack")}} objects).
Methods
This interface inherits methods from its parent, {{domxref("EventTarget")}}.
- {{domxref("MediaStream.addTrack()")}}
- Stores a copy of the {{domxref("MediaStreamTrack")}} given as argument. If the track has already been added to the
MediaStream
object, nothing happens; if the track is in thefinished
state - that is, has already reached its end - the exceptionINVALID_STATE_RAISE
is raised.
- {{domxref("MediaStream.clone()")}}
- Returns a clone of the
MediaStream
object. The clone will, however, have a unique value for {{domxref("MediaStream.id", "id")}}.
- {{domxref("MediaStream.getAudioTracks()")}}
- Returns a list of the {{domxref("MediaStreamTrack")}} objects stored in the
MediaStream
object that have theirkind
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.
- {{domxref("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. - {{domxref("MediaStream.getTracks()")}}
- Returns a list of all {{domxref("MediaStreamTrack")}} objects stored in the
MediaStream
object, regardless of the value of thekind
attribute. The order is not defined, and may not only vary from one browser to another, but also from one call to another.
- {{domxref("MediaStream.getVideoTracks()")}}
- Returns a list of the {{domxref("MediaStreamTrack")}} objects stored in the
MediaStream
object that have theirkind
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.
- {{domxref("MediaStream.removeTrack()")}}
- Removes the {{domxref("MediaStreamTrack")}} given as argument. If the track is not part of the
MediaStream
object, nothing happens; if the track is in thefinished
state - that is, it has already reached its end - the exceptionINVALID_STATE_RAISE
is raised.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Media Capture', '#mediastream', 'MediaStream')}} | {{Spec2('Media Capture')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
getAudioTracks() and getVideoTracks() |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("23.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
id |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("41.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
label and stop() {{deprecated_inline()}} |
{{CompatNo}} [1] | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | |
ended {{deprecated_inline()}} |
{{CompatNo}} [2] | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | |
onaddtrack |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop(50)}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
getAudioTracks() and getVideoTracks() |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("23.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
id |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("41.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
label and stop() {{deprecated_inline()}} |
{{CompatUnknown}} | {{CompatNo}} [1] | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} [1] | |
ended {{deprecated_inline()}} |
{{CompatUnknown}} | {{CompatNo}} [2] | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} [2] | |
onaddtrack |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile(50)}} | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
[1] Deprecated in Chrome 45, removed in Chrome 47.
[2] Deprecated in Chrome 52.
See also
- Using the MediaStream API
- WebRTC API
- Web Audio API
- {{domxref("MediaTrack")}}