Why does setting currentTime of HTML5 video element reset time in Chrome?

前端 未结 6 1621
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 09:57

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


        
相关标签:
6条回答
  • 2020-12-16 10:39

    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());
    
    0 讨论(0)
  • 2020-12-16 10:48

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

    0 讨论(0)
  • 2020-12-16 10:55

    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 讨论(0)
  • 2020-12-16 10:57

    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>

    0 讨论(0)
  • 2020-12-16 10:58

    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.

    0 讨论(0)
  • 2020-12-16 11:01

    It should be

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

    as you have

    <video id="video1" class="video-js vjs-default-skin" muted>
    
    0 讨论(0)
提交回复
热议问题