MediaStream - Web APIs 编辑
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 MediaDevices.getUserMedia()
.
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
MediaStreamTrack
objects).
Properties
This interface inherits properties from its parent, EventTarget
.
MediaStream.active
Read only- A Boolean value that returns
true
if theMediaStream
is active, orfalse
otherwise. MediaStream.ended
Read only- 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 ofMediaStreamTrack.readyState
to see if its value isended
for the track or tracks you want to ensure have finished playing.
MediaStream.id
Read only- A
DOMString
containing 36 characters denoting a universally unique identifier (UUID) for the object.
Event handlers
MediaStream.onaddtrack
- An
EventHandler
containing the action to perform when anaddtrack
event is fired when a newMediaStreamTrack
object is added. MediaStream.onremovetrack
- An
EventHandler
containing the action to perform when aremovetrack
event is fired when aMediaStreamTrack
object is removed from it.
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 theMediaStream
object, nothing happens.
MediaStream.clone()
- Returns a clone of the
MediaStream
object. The clone will, however, have a unique value forid
.
MediaStream.getAudioTracks()
- Returns a list of the
MediaStreamTrack
objects stored in theMediaStream
object that have theirkind
attribute set toaudio
. 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
MediaStreamTrack
objects stored in theMediaStream
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.
MediaStream.getVideoTracks()
- Returns a list of the
MediaStreamTrack
objects stored in theMediaStream
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.
MediaStream.removeTrack()
- Removes the
MediaStreamTrack
given as argument. If the track is not part of theMediaStream
object, nothing happens.
Events
addtrack
- Fired when a new
MediaStreamTrack
object is added.
Also available via theonaddtrack
property. removetrack
- Fired when a
MediaStreamTrack
object has been removed.
Also available via theonremovetrack
property.
active
- Fired when the MediaStream is activated.
inactive
- Fired when the MediaStream is inactivated.
Specifications
Specification | Status | Comment |
---|---|---|
Media Capture and Streams The definition of 'MediaStream' in that specification. | Candidate Recommendation |
Browser compatibility
BCD tables only load in the browser
See also
- Using the MediaStream API
- WebRTC API
- Web Audio API
MediaStreamTrack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论