5.实现原理
Blob() 构造函数var aBlob = new Blob( array, options );
XMLHttpRequest.responseType 这是XHR返回响应的类型
谁知道可以返回那些类型?
XMLHttpRequest.responseType 属性是一个枚举值,返回响应的类型。
它还允许作者将响应类型更改为一个”arraybuffer”, “blob”, “document”, “json”, 或 “text” 。如果将一个空字符串设置为responseType的值,则将其假定为类型“text”。
responseType支持以下几种值:
Value | Data type of response property |
---|---|
“” | DOMString (this is the default value) |
“arraybuffer” | ArrayBuffer |
“blob” | Blob |
“document” | Document |
“json” | JavaScript object, parsed from a JSON string returned by the server |
“text” | DOMString |
“moz-blob” | Used by Firefox to allow retrieving partial Blob data from progress events. This lets your progress event handler start processing data while it’s still being received. |
“moz-chunked-text” | Similar to “text”, but is streaming. This means that the value in response is only available during dispatch of the “progress” event and only contains the data received since the last “progress” event. When response is accessed during a “progress” event it contains a string with the data. Otherwise it returns null.This mode currently only works in Firefox. |
“moz-chunked-arraybuffer” | Similar to “arraybuffer”, but is streaming. This means that the value in response is only available during dispatch of the “progress” event and only contains the data received since the last “progress” event.When response is accessed during a “progress” event it contains a string with the data. Otherwise it returns null.This mode currently only works in Firefox. |
“ms-stream” | Indicates that the response is part of a streaming download. It is supported only for download requests. This mode is available only in Internet Explorer. |
大家会看到
返回的数据类型里有ArrayBuffer和blob。
上代码:
arraybuffer
html:
<video controls></video>
javascript:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37var video = document.querySelector('video');
var assetURL = 'frag_bunny.mp4';
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) {
console.log(buf);
sourceBuffer.addEventListener('updateend', function (_) {
mediaSource.endOfStream();
video.play();
//console.log(mediaSource.readyState); // ended
});
sourceBuffer.appendBuffer(buf);
});
};
function fetchAB (url, cb) {
var xhr = new XMLHttpRequest;
xhr.open('get', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
cb(xhr.response);
};
xhr.send();
};
blob
1 | window.URL = window.URL || window.webkitURL; |
如果遇到相关的问题,欢迎留言,如果时间允许,楼主会尽快回复!