BaseAudioContext: currentTime 屬性

Baseline 已廣泛支援

此特性已得到良好支援,可在多種裝置和瀏覽器版本上使用。自 2021 年 4 月起,所有瀏覽器均已支援此特性。

BaseAudioContext 介面的 currentTime 只讀屬性返回一個雙精度浮點數,表示一個不斷增加的硬體時間戳(以秒為單位),可用於安排音訊播放、視覺化時間軸等。它從 0 開始。

一個浮點數。

示例

js
const audioCtx = new AudioContext();
// Older webkit/blink browsers require a prefix

// …

console.log(audioCtx.currentTime);

時間精度降低

為了提供針對定時攻擊和指紋識別的保護,audioCtx.currentTime 的精度可能會根據瀏覽器設定進行舍入。在 Firefox 中,privacy.reduceTimerPrecision 首選項預設啟用,預設為 2ms。你也可以啟用 privacy.resistFingerprinting,在這種情況下,精度將為 100ms 或 privacy.resistFingerprinting.reduceTimerPrecision.microseconds 的值,取兩者中較大的一個。

例如,在降低時間精度的情況下,audioCtx.currentTime 的結果將始終是 0.002 的倍數,或者在啟用 privacy.resistFingerprinting 的情況下是 0.1(或 privacy.resistFingerprinting.reduceTimerPrecision.microseconds)的倍數。

js
// reduced time precision (2ms) in Firefox 60
audioCtx.currentTime;
// Might be:
// 23.404
// 24.192
// 25.514
// …

// reduced time precision with `privacy.resistFingerprinting` enabled
audioCtx.currentTime;
// Might be:
// 49.8
// 50.6
// 51.7
// …

規範

規範
Web Audio API
# dom-baseaudiocontext-currenttime

瀏覽器相容性

另見