MediaSource
是Media Source Extensions API 表示媒体资源HTMLMediaElement
对象的接口。MediaSource
对象可以附着在HTMLMediaElement
在客户端进行播放。
构造函数
构造并且返回一个新的
MediaSource
的空对象(with no associated source buffers)。
属性
从父接口EventTarget
上继承而来。
返回一个
SourceBufferList
对象,包含了这个MediaSource的
SourceBuffer
的对象列表。Returns a
SourceBufferList
object containing a subset of theSourceBuffer
objects contained withinSourceBuffers
— the list of objects providing the selected video track, enabled audio tracks, and shown/hidden text tracks.返回一个包含当前MediaSource
状态的集合,即使它当前没有附着到一个media元素(closed
),或者已附着并准备接收SourceBuffer
对象 (open
),亦或者已附着但这个流已被MediaSource.endOfStream()
关闭(ended
.)Gets and sets the duration of the current media being presented.
方法
Inherits properties from its parent interface, EventTarget
.
创建一个带有给定MIME类型的新的
SourceBuffer
并添加到MediaSource 的
SourceBuffers
列表。删除指定的
SourceBuffer
从这个MediaSource
对象中的SourceBuffers
列表。Signals the end of the stream.
静态方法
返回一个
Boolean
值表明给定的MIME类型是否被当前的浏览器——这意味着可以成功的创建这个MIME类型的SourceBuffer
对象。
Examples
这个例子尽可能快地逐块加载视频,并在加载好后立刻播放。 This example was written by Nick Desaulniers and can be viewed live here (you can also download the source for further investigation.)
var video = document.querySelector('video');var assetURL = 'frag_bunny.mp4';// Need to be specific for Blink regarding codecs// ./mp4info frag_bunny.mp4 | grep Codecvar mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) { var mediaSource = new MediaSource(); //console.log(mediaSource.readyState); // closed video.src = URL.createObjectURL(mediaSource); mediaSource.addEventListener('sourceopen', sourceOpen);} else { console.error('Unsupported MIME type or codec: ', mimeCodec);}function sourceOpen (_) { //console.log(this.readyState); // open var mediaSource = this; var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec); fetchAB(assetURL, function (buf) { sourceBuffer.addEventListener('updateend', function (_) { mediaSource.endOfStream(); video.play(); //console.log(mediaSource.readyState); // ended }); sourceBuffer.appendBuffer(buf); });};function fetchAB (url, cb) { console.log(url); var xhr = new XMLHttpRequest; xhr.open('get', url); xhr.responseType = 'arraybuffer'; xhr.onload = function () { cb(xhr.response); }; xhr.send();};
说明
Specification | Status | Comment |
---|---|---|
Media Source Extensions MediaSource | Candidate Recommendation | Initial definition. |
浏览器支持情况
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 23-webkit 31 | (Yes) | 25.0 (25.0)[1] 42.0 (42.0) | 11[2] | 15 | 8 |
[1] Available after switching the about:config
preference media.mediasource.enabled
to true
. In addition, support was limited to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist was removed and Media Source Extensions was enabled by default in 42+ for all sites.
[2] Only works on Windows 8+.