Body.arrayBuffer() - Web API 接口参考 编辑
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
Body
上的方法 arrayBuffer()
接受一个 Response
流, 并等待其读取完成. 它返回一个 promise 实例, 并 resolve 一个 ArrayBuffer
对象.
语法
response.arrayBuffer().then(function(buffer) {
// do something with buffer
)};
参数
无。
返回值
A promise that resolves with an ArrayBuffer
.
例子
In our fetch array buffer example (run fetch array buffer live), we have a Play button. When pressed, the getData()
function is run.
In getData()
we create a new request using the Request.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
.
Once 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) {
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');
}
标准
标准 | 状态 | 备注 |
---|---|---|
Fetch arrayBuffer() | Living Standard |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 41[1] 42 | 34[1] 39 (39) | 未实现 | 28[1] | 未实现 |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 |
[1] In Chrome 42, Firefox 34 and Opera 28 support for arrayBuffer()
was hidden behind a preference.
参考
-- NORMAL --如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论