AudioBuffer: thuộc tính duration
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thuộc tính duration của giao diện AudioBuffer trả về một số thực kép biểu diễn thời lượng, tính bằng giây, của dữ liệu PCM được lưu trong bộ đệm.
Giá trị
Một số thực kép.
Ví dụ
js
// Stereo
const channels = 2;
// Tạo một bộ đệm stereo rỗng dài hai giây theo
// tần số lấy mẫu của AudioContext
const frameCount = audioCtx.sampleRate * 2.0;
const myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = () => {
// Điền nhiễu trắng vào bộ đệm;
// chỉ là các giá trị ngẫu nhiên giữa -1.0 và 1.0
for (let channel = 0; channel < channels; channel++) {
// Đây là ArrayBuffer thực sự chứa dữ liệu
const nowBuffering = myArrayBuffer.getChannelData(channel);
for (let i = 0; i < frameCount; i++) {
// Math.random() nằm trong [0; 1.0]
// âm thanh cần nằm trong [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
console.log(myArrayBuffer.duration);
};
Thông số kỹ thuật
| Specification |
|---|
| Web Audio API> # dom-audiobuffer-duration> |