Media Capture and Streams API (Media Stream)

媒体捕获和流 API, often called the Media Streams API or simply MediaStream API , is an API related to WebRTC which provides support for streaming audio and video data. It provides the interfaces and methods for working with the streams and their constituent tracks, the constraints associated with data formats, the success and error callbacks when using the data asynchronously, and the events that are fired during the process.

概念和用法

The API is based on the manipulation of a MediaStream object representing a flux of audio- or video-related data. See an example in Get the video .

A MediaStream consists of zero or more MediaStreamTrack objects, representing various audio or video tracks . Each MediaStreamTrack may have one or more channels . The channel represents the smallest unit of a media stream, such as an audio signal associated with a given speaker, like left or right in a stereo audio track.

MediaStream objects have a single input and a single output . A MediaStream object generated by getUserMedia() is called local , and has as its source input one of the user's cameras or microphones. A non-local MediaStream may be representing to a media element, like <video> or <audio> , a stream originating over the network, and obtained via the WebRTC RTCPeerConnection API, or a stream created using the Web 音频 API MediaStreamAudioSourceNode .

The output of the MediaStream object is linked to a consumer . It can be a media elements, like <audio> or <video> , the WebRTC RTCPeerConnection API or a Web 音频 API MediaStreamAudioSourceNode .

接口

In these reference articles, you'll find the fundamental information you'll need to know about each of the interfaces that make up the Media Capture and Streams API.

Early versions of the Media Capture and Streams API specification included separate AudioStreamTrack and VideoStreamTrack interfaces—each based upon MediaStreamTrack —which represented streams of those types. These no longer exist, and you should update any existing code to instead use MediaStreamTrack directly.

Events

Guides and tutorials

The articles below provide additional guidance and how-to information that will help you learn to use the API, and how to perform specific tasks that you may wish to handle.

Capabilities, constraints, and settings
The twin concepts of constraints and capabilities let the browser and Web site or app exchange information about what constrainable properties the browser's implementation supports and what values it supports for each one. This article discusses capabilities and constraints, as well as media settings, and includes an example we call the Constraint Exerciser .

浏览器兼容性

更新 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
MediaStream Chrome 55
55
21
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 15 IE 不支持 No Opera 42
42
15
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 11 WebView Android 55
55
≤37
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android 55
55
25
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 15 Opera Android 42
42
14
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 11 Samsung Internet Android 6.0
6.0
1.5
Prefixed
Prefixed Implemented with the vendor prefix: webkit
MediaStream() 构造函数 Chrome 21 Edge ≤18 Firefox 44 IE 不支持 No Opera 42 Safari 不支持 No WebView Android 37 Chrome Android 25 Firefox Android 42 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 6.0
active Chrome 45 Edge 12 Firefox 52 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 45 Chrome Android 45 Firefox Android 52 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 5.0
active event Chrome 45 Edge ≤79 Firefox ? IE 不支持 No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 5.0
addTrack Chrome 26 Edge 12 Firefox 44 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 不支持 No Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
addtrack event Chrome Yes Edge 12 Firefox 50 IE 不支持 No Opera 不支持 No Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 50 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android Yes
clone Chrome 45 Edge 12 Firefox 48 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 45 Chrome Android 45 Firefox Android 48 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 5.0
ended
弃用
Chrome 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 52.
Edge ? Firefox 不支持 No IE 不支持 No Opera 不支持 ? — 39 Safari ? WebView Android 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 52.
Chrome Android 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 52.
Firefox Android 不支持 No Opera Android 不支持 ? — 41 Safari iOS ? Samsung Internet Android 不支持 ? — 6.0
注意事项
不支持 ? — 6.0
注意事项 Deprecated in Samsung Internet 6.0.
getAudioTracks Chrome 26 Edge 12 Firefox 22
注意事项
22
注意事项 Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
IE 不支持 No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 22
注意事项
22
注意事项 Prior to Firefox 64, this method returned an array of AudioStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
getTrackById Chrome 26 Edge 12 Firefox 49 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 49 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
getTracks
Chrome 45 Edge 12 Firefox Yes IE 不支持 No Opera Yes Safari Yes WebView Android 45 Chrome Android 45 Firefox Android Yes Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 5.0
getVideoTracks
Chrome 26 Edge 12 Firefox 22
注意事项
22
注意事项 Prior to Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
IE 不支持 No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 22
注意事项
22
注意事项 Prior to Firefox 64, this method returned an array of VideoStreamTrack objects. However, MediaStreamTrack has now subsumed that interface's functionality.
Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
id Chrome 不支持 ? — 54 Edge 不支持 12 — 79 Firefox 41 IE 不支持 No Opera 不支持 ? — 39 Safari Yes WebView Android 不支持 ? — 54 Chrome Android 不支持 ? — 54 Firefox Android 41 Opera Android 不支持 ? — 41 Safari iOS Yes Samsung Internet Android 不支持 ? — 6.0
inactive event Chrome 45 Edge ≤79 Firefox ? IE 不支持 No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 5.0
label
弃用
Chrome 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 45.
Edge ? Firefox ? IE 不支持 No Opera 不支持 No Safari ? WebView Android 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 45.
Chrome Android 不支持 ? — 54
注意事项
不支持 ? — 54
注意事项 Deprecated in Chrome 45.
Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 不支持 ? — 6.0
注意事项
不支持 ? — 6.0
注意事项 Deprecated in Samsung Internet 5.0.
onactive Chrome 45 Edge ≤79 Firefox ? IE 不支持 No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 5.0
onaddtrack Chrome 26 Edge 12 Firefox 50 IE 不支持 No Opera 不支持 No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 50 Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
oninactive Chrome 45 Edge ≤79 Firefox ? IE 不支持 No Opera ? Safari ? WebView Android 45 Chrome Android 45 Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 5.0
onremovetrack Chrome 26 Edge 12 Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari Yes WebView Android 37 Chrome Android 26 Firefox Android 不支持 No Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
removeTrack Chrome 26 Edge 12 Firefox Yes IE 不支持 No Opera Yes Safari Yes WebView Android 37 Chrome Android 26 Firefox Android Yes Opera Android 不支持 No Safari iOS Yes Samsung Internet Android 1.5
removetrack event Chrome Yes Edge 12 Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 不支持 No Opera Android 不支持 No Safari iOS Yes Samsung Internet Android Yes
stop
弃用
Chrome 不支持 ? — 47 Edge 不支持 13 — 79 Firefox ? IE 不支持 No Opera 不支持 No Safari ? WebView Android 不支持 ? — 47 Chrome Android 不支持 ? — 47 Firefox Android ? Opera Android 不支持 No Safari iOS ? Samsung Internet Android 不支持 ? — 5.0

图例

完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅