您正位於 MDN Web Docs 的可編輯版本

以 MDN Web Docs 使用者身分檢視: https://developer.mozilla.org/zh-TW/docs/Web/API/MediaSource

這是一個實驗中的功能
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

Media Source Extensions APIMediaSource 介面代表 HTMLMediaElement 物件的媒體資料來源。一個 MediaSource 物件可以被附加到一個 HTMLMediaElement 以被用戶代理 (user agent) 播放。

建構子 (Constructor)

MediaSource()
建構且回傳一個新的 MediaSource 物件但不與任何來源緩衝 (source buffers) 關聯 (associate)。

屬性 (Properties)

MediaSource.sourceBuffers Read only
回傳一個含有與此 MediaSource 關聯的 SourceBuffer 物件串列的 SourceBufferList 物件。
MediaSource.activeSourceBuffers Read only
回傳一個 SourceBufferList 物件,含有 SourceBuffers 的子集合 SourceBuffer 物件 — 物件的串列提供被選擇的影片軌 (video track), 啟用的音軌 (audio tracks), 以及顯示或隱藏的字軌。
MediaSource.readyState Read only
回傳一個列舉類型表示目前 MediaSource 的狀態:沒有附加到媒體元件 (closed),已經附加且可以接收 SourceBuffer 物件 (open),已經附加但是串流已經經由 MediaSource.endOfStream() 結束 (ended)。
MediaSource.duration
取得或設置現在正被表示的媒體的時間長度。

事件處理函數 (Event handlers)

MediaSource.onsourceclose
sourceclose 事件的事件處理函數。
MediaSource.onsourceended
sourceended 事件的事件處理函數。
MediaSource.onsourceopen
sourceopen 事件的事件處理函數。

方法 (Methods)

從親介面 (parent interface) EventTarget 繼承屬性。

MediaSource.addSourceBuffer()
依據指定的 MIME 類型建立一個新的 SourceBuffer 且將其加入 MediaSource 的 SourceBuffers 串列。
MediaSource.clearLiveSeekableRange()
清除先前藉由呼叫 setLiveSeekableRange() 所設定的可尋找範圍。
MediaSource.endOfStream()
示意串流的結束。
MediaSource.removeSourceBuffer()
從與此 MediaSource 物件關聯的 SourceBuffers 串列移除指定的 SourceBuffer
MediaSource.setLiveSeekableRange()
設定使用者可以在媒體元素中的可尋找範圍。

靜態方法 (Static methods)

MediaSource.isTypeSupported()
回傳一個 Boolean 值表示指定的 MIME 類型是否被現在的用戶代理支援 — 意即可否成功的為該 MIME 類型建立 SourceBuffer 物件。

範例

以下簡單的範例儘快的載入一個個影片塊 (chunk) 且儘快的播放。這個範例是由 Nick Desaulniers 所撰寫且可以在此實際觀看(您也可以下載原始碼以更進一步研究)

var video = document.querySelector('video');

var assetURL = 'frag_bunny.mp4';
// Need to be specific for Blink regarding codecs
// ./mp4info frag_bunny.mp4 | grep Codec
var 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();
};

規格

規格 狀態 註釋
Media Source Extensions
The definition of 'MediaSource' in that specification.
Recommendation Initial definition.

相容性表格

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
MediaSource
Experimental
Chrome Full support 31
Full support 31
No support 23 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11Opera Full support 18
Full support 18
No support 15 — 18
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 31
Full support 31
No support 25 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Firefox Android Full support 41Opera Android Full support 18
Full support 18
No support 14 — 18
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Safari iOS Full support 8Samsung Internet Android Full support 2.0
Full support 2.0
No support 1.5 — 2.0
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
MediaSource() constructor
Experimental
Chrome Full support 31
Full support 31
No support 23 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
activeSourceBuffers
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
addSourceBuffer
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
clearLiveSeekableRange
Experimental
Chrome Full support 62Edge Full support 17Firefox No support NoIE No support NoOpera Full support 49Safari No support NoWebView Android Full support 62Chrome Android Full support 62Firefox Android ? Opera Android Full support 46Safari iOS No support NoSamsung Internet Android Full support 8.0
duration
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
endOfStream
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
isTypeSupported
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
onsourceclose
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
onsourceended
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
onsourceopen
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
readyState
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
removeSourceBuffer
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
setLiveSeekableRange
Experimental
Chrome Full support 62Edge Full support 17Firefox No support NoIE No support NoOpera Full support 49Safari No support NoWebView Android Full support 62Chrome Android Full support 62Firefox Android ? Opera Android Full support 46Safari iOS No support NoSamsung Internet Android Full support 8.0
sourceBuffers
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

相關資料

文件標籤與貢獻者

此頁面的貢獻者: aChinKaiWu, mdnwebdocs-bot
最近更新: aChinKaiWu,