当音频元素暂停时,AnalyserNode会继续发送数据

AnalyserNode keeps sending data when Audio element is paused

本文关键字:继续 数据 AnalyserNode 音频 元素 暂停      更新时间:2023-09-26

我正在使用Web Audio API,有些行为我无法理解。

var audio = document.querySelector('audio');
var context = new AudioContext();
var source = context.createMediaElementSource(audio);
var analyser = context.createAnalyser();
source.connect(analyser);
source.connect(context.destination);
setInterval(function() {
    var freqDomain = new Float32Array(analyser.frequencyBinCount);
    analyser.getFloatFrequencyData(freqDomain);
    console.log(freqDomain);
},1000);

当我暂停 Audio 元素时,控制台会不断向我显示来自分析器的数据(并且数据正在更改)。为什么声音暂停时它会不断发送数据?

我认为

这可能是因为您的AnalyserNode smoothingTimeConstant,默认为0.8

我的猜测是,由于这种随时间推移的平均值,当您暂停<audio>元素时,值将逐渐衰减到-Infinity

无论如何,这只是一个猜测,但我会说我有大约95%的把握。您可以非常轻松地验证它是否设置analyser.smoothingTimeConstant = 0并查看行为是否仍然存在。

哦,这是规范相关部分的链接:https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#dfn-smoothingTimeConstant