AudioBuffer: свойство duration
Базовая Широко доступна
Эта функция хорошо зарекомендовала себя и работает на многих устройствах и версиях браузеров. Она доступна в браузерах с апреля 2021 года.
Свойство duration интерфейса AudioBuffer возвращает число с плавающей запятой, представляющее продолжительность, в секундах, PCM-данных, хранящихся в буфере.
Значение
Число с плавающей запятой.
Примеры
// Stereo
const channels = 2;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
const frameCount = audioCtx.sampleRate * 2.0;
const myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = () => {
// Fill the buffer with white noise;
// just random values between -1.0 and 1.0
for (let channel = 0; channel < channels; channel++) {
// This gives us the actual ArrayBuffer that contains the data
const nowBuffering = myArrayBuffer.getChannelData(channel);
for (let i = 0; i < frameCount; i++) {
// Math.random() is in [0; 1.0]
// audio needs to be in [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
console.log(myArrayBuffer.duration);
};
Спецификации
| Спецификация |
|---|
| Web Audio API # dom-audiobuffer-duration |
Совместимость с браузерами
| Рабочий стол | Мобильный | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox для Android | Opera Android | Safari на IOS | Samsung Internet | WebView Android | |
duration |
14 | 12 | 25 | 15 | 6 | 18 | 25 | 14 | 6 | 1.0 | 4.4.3 |
См. также
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer/duration