41

I want to set the time position of a video in HTML5. The time should be set like this:

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
}

And the video is embedded like this:

<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>

But for some reason, this always just resets currentTime to 0 in Chrome.

Why gets the time reset when setting currentTime? And how can I set currentTime correctly?

user2212461
  • 3,105
  • 8
  • 49
  • 87
  • 2
    Is the video itself, visually, also going back to start or is it just the console.log showing 0? –  Apr 22 '16 at 02:17
  • 1
    Yes, any ideas why and what I am doing wrong? – user2212461 Apr 22 '16 at 02:20
  • 1
    The snippet seem correct to me - setting currentTime is also async but shouldn't matter here (as the video resets visually). The problem could be related to other parts not shown here. Do you have the possibility to set up a fiddle/inline snippet that can reproduce the problem? –  Apr 22 '16 at 02:23
  • 3
    I found out the code above works in firefox. Is there anything in Chrome that I might have to do differently? – user2212461 Apr 22 '16 at 02:41
  • Can you reproduce this with an other file? – Kaiido Apr 22 '16 at 03:09
  • @user2212461 it could of course be a bug. You could try with Chrome Canary as well as newer Opera (which uses the same Blink engine) to see if the problem can be reproduced or if it's just in the current version of Chrome. I'm afraid though that without any code that reproduces the problem we won't be able to help. But with the limited information I would probably vote for bug, although, I would also be open to a "race condition" event wise (conflicting handling in some event callback if there are several). –  Apr 22 '16 at 14:53
  • now with in version 52.0.2743.82 (64-bit) it is working as expected. no change in code, just update of chrome solved the issue for me. – Petr Marek Jul 31 '16 at 08:05
  • I have this same problem. – Manu Masson Sep 12 '16 at 09:59
  • @PetrMarek - I still have the same issue in Chrome 55.0.2883.95 Mac version. Have you tested in that version? – Ng2-Fun Jan 03 '17 at 20:00
  • I have the same problem in Chrome. In Firefox, it works. – Julisch Nov 05 '18 at 23:45
  • 1
    Just confirming I have the same problem in Chrome (71.0.3578.98), but it works in Firefox. – lucasvw Jan 23 '19 at 19:55
  • I have the same issues, the exact problematic described behaviour with audio in Chromium (73.0.3683.86), Opera (62.0.3331.18), but better in Firefox (67.0). To me it seems that the file type/audio codec is the root cause for the difference in the browsers. Using ogg, this works fine with Firefox but not with the others, while mp3 also troubles Firefox. Have not arrived at a reproducible condition to make this a question. (btw., passing currentTime a string doesn't change anything so can it be you used a different file format later?) – AdagioMolto Jun 27 '19 at 14:15
  • Could you reproduce it in [CodeSandbox](https://codesandbox.io/) or other online environments? – Hao-Cher Hong Feb 04 '20 at 10:11

10 Answers10

17

This is caused in many cases by the server not replying with an Accept-Ranges header. For an obscure, only-Google-developer-relevant reason (the FF folks don't seem to be so offended by a server replying without an Accept-Ranges header), they refuse to implement seeking when such a header is not provided. You can add one very easily (at least for testing) by using an nginx proxy and adding proxy_force_ranges on; (server or location both work).

AntonOfTheWoods
  • 809
  • 13
  • 17
  • Thank you. This fixed it for my situation and makes total sense. – Ting Qian Apr 17 '21 at 02:29
  • 1
    To add more detail to this answer: the `Accept-Ranges` header is not missing when responding the video, it's missing when responding the HTML file, because then the Client knows that the server supports range-requests **before** making the video request. – Karsten Apr 30 '21 at 06:47
8

I finally figured out an answer! Chrome requires you to set the currentTime as a String, and not as a number.

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();
<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>
lucasvw
  • 1,345
  • 17
  • 36
  • 10
    This is definitely not the case in my current version of Chrome (73.0.3683.75) – Tyguy7 Mar 28 '19 at 21:32
  • 1
    Maybe they've fixed it by now, but it certainly functioned like that in my old version of Chrome. I should've noted what version I was using then. – lucasvw Mar 28 '19 at 22:30
  • 4
    Unfortunately, this did not solve the issue for me under Chrome 80.0.3987.162 (64 bit under Mint). I would've gone down to the nearest Google office and blown raspberries at them if it did, though. – Michael Scheper Apr 03 '20 at 22:09
  • 1
    Version 90.0.4430.212 (Official Build) (64-bit) Win10 -- Using a string is still not working here either. – Darren S May 18 '21 at 10:04
  • 1
    hahaha that's funny.. my typescript won't let me set a string to a number-property :D ... – Matthias Burger Jul 15 '21 at 11:52
5

This is a bug in Chrome, which apparently only happens with 'some' videos. Similar bug reports have been filed and closed numerous times in the past, so I suggest we keep a watch on it, so it doesn't get closed again until the issue is actually fixed.

Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
  • 3
    Absolutely typical Google developer reaction "[insert obscure reason] so you should all do [insert annoying and case-specific action]". Almost immediately set as wontfix. – AntonOfTheWoods Jan 20 '21 at 01:26
4

It has probably already been addressed, but it's a Chrome "bug" that they could fix like FF but #wontfix I guess. It's a server-side fix so you would want to look at making sure you pass the headers right.

It's wasteful as heck but by setting the Accept-Ranges to bytes (be sure to include the content length and mime type as well), it will try to redownload the file every time you zoom around from byte zero all the way to where you're landing -- so it lets you jump around but at a huge cost.

    header('Accept-Ranges: bytes');

I would advise you read through this question's answers and try to do ranges all the way right so that you're not wasting as much bandwidth:

Resumable downloads when using PHP to send the file?

Also, more reference:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#range_requests

Darren S
  • 516
  • 4
  • 8
1

You can add preload="auto" and the browser will try to download the file as soon as possible.

https://developer.mozilla.org/en-US/docs/Web/Guide/Audio_and_video_delivery/Cross-browser_audio_basics

Worked for me.

0

Seems you're using video.js. If so, get and set currentTime via VideoJsPlayer.currentTime() method.

For example:

const player = videojs(document.getElementById("video1"));

// read currentTime
console.log(player.currentTime());

//set currentTime
player.currentTime(10.0);

// read currentTime
console.log(player.currentTime());
Hao-Cher Hong
  • 230
  • 2
  • 6
0

In my case I had to update video serving server to return response with Status Code 206 and Content-Range and Content-Length headers

Adam Małek
  • 293
  • 1
  • 5
0

I came across the same problem. Here's what happened in the bottom line.

  1. If you are writing code in an integrated development environment, then open your HTML file directly from a hard disk besides the IDE server - This happened to me, the server did not let you set an arbitrary time during video playback.
  2. You can check if the browser can perform this operation alert ("Start:" + player.seekable.start (0) + "End:" + player.seekable.end (0)); player.seekable returns a TimeRanges object: Definition and Usage The seekable property returns a TimeRanges object. The TimeRanges object represents ranges of the audio / video that are available for seeking for user. A seekable range is a time-range of audio / video where the user can seek (move playback position) to. For non-streaming videos it is often possible to seek anywhere in the video even before it has been buffered. Note: This property is read-only. www.w3schools.com article
0

The solution for me was to first send the following headers from my server:

Accept-Ranges: none,
Content-Length: 123456,
Content-Type: audio/mpeg

And then use a MediaSource object:

const mediaSource = new MediaSource();
audio.src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', sourceOpen, { once: true });

function sourceOpen() {
    URL.revokeObjectURL(audio.src);
    const sourceBuffer = mediaSource.addSourceBuffer('audio/mpeg');

    fetch('https://example.com/audio.mp3')
    .then(response => response.arrayBuffer())
    .then(data => {
        sourceBuffer.appendBuffer(data);
    })
    .catch(error => {
        console.log(error);
    });
}

This will force the browser to download the whole file, which may not be desirable depending on your use case.

Pier
  • 10,298
  • 17
  • 67
  • 113
-3

It should be

var video = document.getElementById("video1");

as you have

<video id="video1" class="video-js vjs-default-skin" muted>
Rob Quincey
  • 2,834
  • 2
  • 38
  • 54
neophyte
  • 6,540
  • 2
  • 28
  • 43
  • 2
    Although your answer does point out a problem in the code, it does not answer the main problem of the question. – Idris Dopico Peña Mar 04 '19 at 11:15
  • @IdrisDopicoPeña: It is a real bug, though, so I don't think it's fair to downvote this answer. It would be much more constructive to just edit the question. – Michael Scheper Apr 03 '20 at 22:03
  • @MichaelScheper - I only gave my feedback, I did not downvote this answer :). Even so, he only mentioned the fix (read: just gave the answer), instead of giving the feedback that was needed. – Idris Dopico Peña Apr 10 '20 at 13:51