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

浏览器兼容性

另见