HTML5 play()不支持倒放,只能正向播放;实现倒放需手动递减currentTime,配合requestAnimationFrame或setInterval控制节奏,步长≤0.04,并在currentTime≤0时暂停并归零。
play() 本身不支持倒放直接调用 play() 永远只会正向播放——这是规范决定的,不是浏览器兼容问题。HTML5 和 元素没有内置“倒放”模式,play() 的行为定义就是从当前 currentTime 向后推进。
currentTime
核心思路是:定时减少 currentTime,同时保持媒体处于播放状态(避免因暂停导致时间冻结)。关键点在于控制节奏和边界处理:
requestAnimationFrame 或 setInterval 持续递减 currentTime,步长建议 ≤ 0.04(对应约 25fps)以保证流畅currentTime 是否 ≤ 0,到头就 pause() 并设为 currentTime = 0
currentTime 可能触发 seeking 事件,但不影响倒放逻辑;若媒体未加载完成(readyState ),设置 currentTime 会无效或抛错
play() 是误导性操作很多教程写“先 play() 再倒放”,其实 play() 在这里只起“解除播放限制”作用(如用户手势触发策略),它本身不参与倒放过程。真正驱动倒放的是你写的定时器逻辑:
let isReversing = false;
function startReverse() {
if (video.paused) video.play().catch(e => console.warn("play() failed:", e));
isReversing = true;
reverseLoop();
}
function reverseLoop() {
if (!isReversing) return;
video.currentTime = Math.max(0, video.currentTime - 0.04);
if (video.currentTime <= 0) {
video.pause();
video.currentTime = 0;
isReversing = false;
} else {
requestAnimationFrame(reverseLoop);
}
}iOS Safari 和部分安卓 WebView 对自动播放/后台播放限制极严,即使用户已交互过,play() 仍可能被静音或拒绝。这意味着:
autoplay + muted 绕过(倒放时声音方向异常,静音反而掩盖问题)playbackRate 设为负值(有些老方案试图用 playbackRate = -1,实测无效且被规范废弃)倒放本质是“模拟”,不是原生能力。最易被忽略的是时间精度——currentTime 是双精度浮点数,反复增减会产生累积误差,实际应用中建议定期对齐关键帧或使用整数毫秒做校准。
