How to play a video from the nth second? [duplicate]

萝らか妹 提交于 2019-12-23 06:45:05

问题


Let’s say I have a 4 minute video and I want it to start playing from the 25th second and not from the beginning.

<video autoplay loop id="bgvid">
  <source src="back.mp4" type="video/mp4">
</video>

回答1:


Try appending #t=TIME

<video autoplay loop id="bgvid">
  <source src="back.mp4#t=25" type="video/mp4">
</video>

W3.org: Media Fragments URI - http://www.w3.org/TR/media-frags/




回答2:


document.getElementById('bgvid').addEventListener('loadedmetadata', function() {
  this.currentTime = 25;
}, false);



回答3:


Yes Mohit, I've been looking for a very same functionality earlier on, according to this question, you can try this snippet of javascript code, it worked for me; [it is also a method to stop the video at a certain time]

function playVideo() {
    var starttime = 2;  // start at 2 seconds
    var endtime = 4;    // stop at 4 seconds

    var video = document.getElementById('player1');

    //handler should be bound first
    video.addEventListener("timeupdate", function() {
       if (this.currentTime >= endtime) {
            this.pause();
        }
    }, false);

    //suppose that video src has been already set properly
    video.load();
    video.play();    //must call this otherwise can't seek on some browsers, e.g. Firefox 4
    try {
        video.currentTime = starttime;
    } catch (ex) {
        //handle exceptions here
    }
}


来源:https://stackoverflow.com/questions/31978594/how-to-play-a-video-from-the-nth-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!