arrayBuffer()
方法在
Body
mixin takes a
响应
stream and reads it to completion. It returns a promise that resolves with an
ArrayBuffer
.
response.arrayBuffer().then(function(buffer) {
// do something with buffer
});
None.
A promise that resolves with an
ArrayBuffer
.
In our
fetch array buffer live
, we have a Play button. When pressed, the
getData()
function is run. Note that before playing full audio file will be downloaded. If you need to play ogg during downloading (stream it) - consider
HTMLAudioElement
:
new Audio("music.ogg").play();
在
getData()
we create a new request using the
Request()
constructor, then use it to fetch an OGG music track. We also use
AudioContext.createBufferSource
to create an audio buffer source. When the fetch is successful, we read an
ArrayBuffer
out of the response using
arrayBuffer()
, decode the audio data using
AudioContext.decodeAudioData
, set the decoded data as the audio buffer source's buffer (
source.buffer
), then connect the source up to the
AudioContext.destination
.
一旦
getData()
has finished running, we start the audio source playing with
start(0)
, then disable the play button so it can't be clicked again when it is already playing (this would cause an error.)
function getData() {
source = audioCtx.createBufferSource();
var myRequest = new Request('viper.ogg');
fetch(myRequest).then(function(response) {
return response.arrayBuffer();
}).then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
source.buffer = decodedData;
source.connect(audioCtx.destination);
});
});
};
// wire up buttons to stop and play audio
play.onclick = function() {
getData();
source.start(0);
play.setAttribute('disabled', 'disabled');
}
Response()
constructor accepts
File
s and
Blob
s, so it may be used to read a
File
into other formats.
function readFile(file) {
return new Response(file).arrayBuffer();
}
<input type="file" onchange="readFile(this.files[0])">
| 规范 | 状态 | 注释 |
|---|---|---|
|
Fetch
The definition of 'arrayBuffer()' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
arrayBuffer
|
Chrome
42
|
Edge ≤18 |
Firefox
39
|
IE No |
Opera
29
|
Safari 10 | WebView Android No | Chrome Android 42 | Firefox Android No |
Opera Android
29
|
Safari iOS Yes | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。