MediaStreamTrack

MediaStreamTrack interface represents a single media track within a stream; typically, these are audio or video tracks, but other track types may exist as well.

特性

In addition to the properties listed below, MediaStreamTrack has constrainable properties which can be set using applyConstraints() and accessed using getConstraints() and getSettings() 。见 Capabilities, constraints, and settings to learn how to correctly work with constrainable properties. Not doing so correctly will result in your code being unreliable.

MediaStreamTrack.contentHint
A string that may be used by the web application to provide a hint as to what type of content the track contains to guide how it should be treated by API consumers.
MediaStreamTrack.enabled
A Boolean whose value of true if the track is enabled, that is allowed to render the media source stream; or false if it is disabled, that is not rendering the media source stream but silence and blackness. If the track has been disconnected, this value can be changed but has no more effect.
注意: You can implement standard "mute" functionality by setting enabled to false muted property refers to a condition in which there's no media because of a technical issue.
MediaStreamTrack.id 只读
返回 DOMString containing a unique identifier (GUID) for the track; it is generated by the browser.
MediaStreamTrack.isolated 只读
Returns a Boolean value which is true if the track is isolated ; that is, the track cannot be accessed by the document that owns the MediaStreamTrack . This happens when the peerIdentity property is set, or if the track comes from a cross-origin source.
MediaStreamTrack.kind 只读
返回 DOMString 设为 "audio" if the track is an audio track and to "video" , if it is a video track. It doesn't change if the track is deassociated from its source.
MediaStreamTrack.label 只读
返回 DOMString containing a user agent-assigned label that identifies the track source, as in "internal microphone" . The string may be left empty and is empty as long as no source has been connected. When the track is deassociated from its source, the label is not changed.
MediaStreamTrack.muted 只读
Returns a Boolean value indicating whether the track is unable to provide media data due to a technical issue.
注意: You can implement standard "mute" functionality by setting enabled to false , and unmute the media by setting it back to true again.
MediaStreamTrack.readonly 只读
Returns a Boolean value with a value of true if the track is readonly (such a video file source or a camera that settings can't be modified), false 否则。
MediaStreamTrack.readyState 只读
Returns an enumerated value giving the status of the track. This will be one of the following values:
  • "live" which indicates that an input is connected and does its best-effort in providing real-time data. In that case, the output of data can be switched on or off using the enabled 属性。
  • "ended" which indicates that the input is not giving any more data and will never provide new data.
MediaStreamTrack.remote 只读
Returns a Boolean with a value of true if the track is sourced by a RTCPeerConnection , false 否则。

方法

MediaStreamTrack.applyConstraints()
Lets the application specify the ideal and/or ranges of acceptable values for any number of the available constrainable properties of the MediaStreamTrack .
MediaStreamTrack.clone()
Returns a duplicate of the MediaStreamTrack .
MediaStreamTrack.getCapabilities()
Returns the a list of constrainable properties available for the MediaStreamTrack .
MediaStreamTrack.getConstraints()
返回 MediaTrackConstraints object containing the currently set constraints for the track; the returned value matches the constraints last set using applyConstraints() .
MediaStreamTrack.getSettings()
返回 MediaTrackSettings object containing the current values of each of the MediaStreamTrack 's constrainable properties.
MediaStreamTrack.stop()
Stops playing the source associated to the track, both the source and the track are deassociated. The track state is set to ended .

Events

监听这些事件使用 addEventListener() 或通过把事件监听器赋值给 on eventname property of this interface:

ended
Sent when playback of the track ends (when the value readyState changes to ended ).
Also available using the onended event handler property.
mute
Sent to the MediaStreamTrack when the value of the muted property is changed to true , indicating that the track is unable to provide data temporarily (such as when the network is experiencing a service malfunction).
Also available using the onmute event handler property.
isolationchange
Sent whenever the value of the isolated property changes due to the document gaining or losing permission to access the track.
Also available through the onisolationchange event handler property.
overconstrained
Sent when the constraints specified for the track cause the track to be incompatible and therefore to not be used.
Also available through the onoverconstrained event handler property.
unmute
Sent to the track when data becomes available again, ending the muted state.
Also available through the onunmute event handler property.

规范

规范 状态 Comment
Identity for WebRTC
The definition of 'Isolated tracks' in that specification.
候选推荐 Additional properties for isolated track support
媒体捕获和流
The definition of 'MediaStreamTrack' 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
MediaStreamTrack Chrome 29 Edge 12 Firefox 22 IE No Opera 16 Safari 11 WebView Android ≤37 Chrome Android 29 Firefox Android 22 Opera Android 16 Safari iOS 11 Samsung Internet Android 2.0
applyConstraints Chrome 63 Edge 12 Firefox 50 IE No Opera 46 Safari Yes WebView Android 63 Chrome Android 63 Firefox Android 50 Opera Android 43 Safari iOS Yes Samsung Internet Android 7.0
clone Chrome 45 Edge 12 Firefox 48 IE No Opera 32 Safari Yes WebView Android 45 Chrome Android 45 Firefox Android Yes Opera Android 32 Safari iOS Yes Samsung Internet Android 5.0
contentHint Chrome 60 Edge ≤79 Firefox ? IE No Opera 47 Safari ? WebView Android 60 Chrome Android 60 Firefox Android ? Opera Android 44 Safari iOS ? Samsung Internet Android 8.0
enabled Chrome Yes Edge 12 Firefox 24 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 24 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
ended event Chrome Yes Edge 12 Firefox 50 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
getCapabilities Chrome 66 Edge 12 Firefox No IE No Opera 53 Safari Yes WebView Android 66 Chrome Android 66 Firefox Android No Opera Android 47 Safari iOS Yes Samsung Internet Android 9.0
getConstraints Chrome 53 Edge 12 Firefox 50 IE No Opera Yes Safari Yes WebView Android 53 Chrome Android 52 Firefox Android 50 Opera Android Yes Safari iOS No Samsung Internet Android 6.0
getSettings Chrome 61 Edge 12 Firefox 50 IE No Opera 48 Safari Yes WebView Android 61 Chrome Android 61 Firefox Android 50 Opera Android 45 Safari iOS 11 Samsung Internet Android 8.0
getSources
弃用
Chrome ? — 56 Edge ? Firefox No IE No Opera ? — 43 Safari ? WebView Android ? — 56 Chrome Android 56 Firefox Android ? Opera Android ? — 43 Safari iOS ? Samsung Internet Android 6.0
id Chrome Yes Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
isolated Chrome ? Edge ? Firefox No IE No Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android No Opera Android ? Safari iOS ? Samsung Internet Android ?
kind Chrome Yes Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
label Chrome Yes Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
mute event Chrome Yes Edge 12 Firefox 59 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 59 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
muted Chrome Yes Edge 12 Firefox 59 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 59 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onended Chrome Yes Edge 12 Firefox 50 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onisolationchange Chrome ? Edge ? Firefox No IE No Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android No Opera Android ? Safari iOS ? Samsung Internet Android ?
onmute Chrome Yes Edge 12 Firefox 59 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 59 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onoverconstrained
弃用 非标
Chrome Yes Edge 12 Firefox Yes IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android ? Opera Android ? Safari iOS Yes Samsung Internet Android Yes
onunmute Chrome Yes Edge 12 Firefox 59 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 59 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
readonly
弃用
Chrome ? Edge 12 Firefox ? IE No Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
readyState Chrome Yes Edge 12 Firefox 50 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
remote
弃用 非标
Chrome 48 — 59 Edge 12 — 79 Firefox Yes IE No Opera 35 — 46 Safari ? WebView Android 48 — 59 Chrome Android 48 — 59 Firefox Android Yes Opera Android 35 — 43 Safari iOS ? Samsung Internet Android 5.0 — 7.0
stop Chrome 61 Edge 12 Firefox 34 IE No Opera 45 Safari Yes WebView Android 61 Chrome Android 61 Firefox Android 34 Opera Android 43 Safari iOS Yes Samsung Internet Android 8.0
unmute event Chrome Yes Edge 12 Firefox 59 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 59 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。

另请参阅