为什么在Chrome中设置HTML5视频元素的currentTime重置时间?

use*_*461 14 html javascript google-chrome html5-video

我想在HTML5中设置视频的时间位置.时间应该设置如下:

function settime(){
    var video = document.getElementById("video1");
    console.log(video.currentTime); //----->output for example 15.3
    video.currentTime = 10.0;
    console.log(video.currentTime);//----->>output always 0
}
Run Code Online (Sandbox Code Playgroud)

视频嵌入如下:

<button onclick="settime();">Set Time</button>
<div class="container">
<video id="video1" class="video-js vjs-default-skin" muted>
     <source src="video.m4v" type="video/mp4" />
     HTML5 Video is required for this example.
</video>
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,这总是在Chrome中将currentTime重置为0.

设置currentTime时为什么要重置时间?如何正确设置currentTime?

luc*_*svw 8

我终于找到了答案!Chrome 要求您将 currentTime 设置为字符串,而不是数字。

function settime() {
    var video = document.getElementById("video1");
    console.log(video.currentTime); // output 0
    video.currentTime = 10.0;
    console.log(video.currentTime); // output 0 for some chrome users or 10 for firefox and others
    video.currentTime = "10.0";
    console.log(video.currentTime); // output 10
}

settime();
Run Code Online (Sandbox Code Playgroud)
<audio id="video1" src="https://sample-videos.com/audio/mp3/crowd-cheering.mp3" controls></audio>
<div>Click play to start the audio at 10 s.</div>
Run Code Online (Sandbox Code Playgroud)

  • 在我当前版本的 Chrome (73.0.3683.75) 中绝对不是这种情况 (6认同)
  • 不幸的是,这并没有解决我在 Chrome 80.0.3987.162(Mint 下的 64 位)下的问题。不过,如果真的发生的话,我会去最近的谷歌办公室,对他们大发雷霆。 (3认同)
  • 哈哈哈,这很有趣..我的打字稿不允许我将字符串设置为数字属性:D ... (2认同)

Ant*_*ods 6

这在很多情况下是由服务器没有回复Accept-Ranges标头引起的。出于一个晦涩的、仅与 Google 开发人员相关的原因(FF 人员似乎并没有因为没有Accept-Ranges标头的服务器回复而受到冒犯),当没有提供这样的标头时,他们拒绝实施搜索。您可以通过使用 nginx 代理并添加proxy_force_ranges on;(服务器或位置都有效)来非常轻松地添加一个(至少用于测试)。


Mic*_*per 5

这是 Chrome 中的一个错误,显然只发生在“某些”视频上。类似的错误报告过去已多次提交和关闭,因此我建议我们密切关注,以便在问题真正得到解决之前不会再次关闭。

  • 绝对典型的谷歌开发者反应“[插入晦涩的原因]所以你们都应该做[插入烦人的和针对具体情况的操作]”。几乎立即设置为 wontfix。 (4认同)

neo*_*yte -3

它应该是

var video = document.getElementById("video1");
Run Code Online (Sandbox Code Playgroud)

正如你所拥有的

<video id="video1" class="video-js vjs-default-skin" muted>
Run Code Online (Sandbox Code Playgroud)

  • 虽然你的回答确实指出了代码中的问题,但它并没有回答问题的主要问题。 (2认同)