HTMLMediaElement: currentTime 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

HTMLMediaElement 介面的 currentTime 屬性用於指定當前的播放時間,單位為秒。

更改 currentTime 的值會將媒體定位到新的時間點。

一個雙精度浮點數值,表示當前的播放時間,單位為秒。

如果媒體尚未開始播放,currentTime 的值表示呼叫 play() 方法後,媒體將開始播放的時間位置。

設定 currentTime 為新值會將媒體定位到指定的時間點,前提是媒體可用。

對於沒有已知時長的媒體(例如直播流媒體),瀏覽器可能無法獲取已從媒體緩衝區過期的部分。此外,時間軸不從 0 秒開始的媒體無法定位到其時間軸最早時間之前的某個時間點。

可以使用 duration 屬性來確定媒體的時長(秒)。

示例

js
const video = document.createElement("video");
console.log(video.currentTime);

用法說明

時間精度降低

為了防止定時攻擊和指紋識別video.currentTime 的精度可能會根據瀏覽器設定進行舍入。在 Firefox 中,privacy.reduceTimerPrecision 首選項預設啟用,預設值為 2ms。您也可以啟用 privacy.resistFingerprinting,在這種情況下,精度將為 100ms 或 privacy.resistFingerprinting.reduceTimerPrecision.microseconds 的值,以較大者為準。

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

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

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

規範

規範
HTML
# dom-media-currenttime-dev

瀏覽器相容性

另見