Search code examples
javascriptphpvideo.js

video.js starting video after 1 minute


I have this piece of code that runs a video on my page, I would like to start the video after 60 seconds, but how do i do this?

                    <video id="video1" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="720" height="576" poster="<?php echo $pic1; ?>" data-setup="{}">
            <source src="<?php echo $filename; ?>" type='video/mp4'>
            <track kind="chapters" src="<?php echo $chapters; ?>" srclang="en" label="English" default="default" >
            <p class="vjs-no-js">
              To view this video please enable JavaScript, and consider upgrading to a web browser that
              <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
            </p>
          </video>

Solution

  • In your javascript file:

    const mainVideo = document.getElementById("video1");
    let playVideoAfter = 60; // seconds
    
    setTimeout(() => mainVideo.play(), playVideoAfter * 1000);
    

    This code selects the video, then calls the play() function on it after 60000 milliseconds (60 seconds)