Play full HTML5 video and then loop a section of it

后端 未结 5 1135
不知归路
不知归路 2021-02-05 15:20

I\'m trying to play an 8.6 second video once completely, and then loop a small section of the video infinitely, to keep the illusion of a never-ending video. So far I\'ve looked

相关标签:
5条回答
  • 2021-02-05 15:45

    Try the following, to 'rewind' it as soon as it ends:

    vidElem.addEventListener("ended", function () {
            vidElem.currentTime = 2.5;
            vidElem.play();
    }, false);
    

    Updated fiddle: http://jsfiddle.net/Lt4n7/1/

    0 讨论(0)
  • 2021-02-05 15:47

    I just had to deal with the same problem and noticed the same issues with flickering. Here was my solution:

    • Get 2 videos (or sets of videos) - one for the non-looped section, the other for the looped section
    • Create 2 video elements
    • set the looping element to 'display:none'

    Then just capture the ended event and swap display status (example uses jquery but you could use 'style.display="none/block"' just as easily:

    VideoPlayer1 = document.getElementById('video1');
    VideoPlayer2 = document.getElementById('video2');
    VideoPlayer1.addEventListener('ended', videoLooper, false);
    
    function videoLooper()
    {
        VideoPlayer2.play();
        $(VideoPlayer2).show();
        $(VideoPlayer1).hide();
    }
    
    0 讨论(0)
  • 2021-02-05 15:51

    Your code is fine, the problem is with your MP4 file! Try using a much smaller video like this one ( http://www.w3schools.com/tags/movie.mp4 ) to confirm the issue is not with your code.

    So how can you achieve the same result but with large videos files? You will need two video files:

    • video1 is the main video
    • video2 is the looping video

    Remember: HTML5 video has no problem playing and looping large video files so we will use this method to play the videos.

    In the example below we will play the first video and when it finishes we will execute a function to hide video1 and then show/play video2. (Video 2 is already set to loop)

    Don't forget to load JQuery in your head otherwise this will not work.

    <video id="video1" width="1080" height="568" poster="movie.png" autoplay onended="run()">
      <source src="movie.webm" type="video/webm">
      <source src="movie.ogg" type="video/ogg">
      <source src="movie.mp4" type="video/mp4">
      <object data="movie.mp4" width="1080" height="568">
        <embed width="1080" height="568" src="movie.swf">
      </object>
    Optional test to be displayed if the browser doesn't support the video tag (HTML 5)
    </video>
    
    
    <video id="video2" width="1080" height="568" poster="loop.png" loop>
      <source src="loop.webm" type="video/webm">
      <source src="loop.ogg" type="video/ogg">
      <source src="loop.mp4" type="video/mp4">
      <object data="loop.mp4" width="1080" height="568">
        <embed width="1080" height="568" src="loop.swf">
      </object>
    Optional test to be displayed if the browser doesn't support the video tag (HTML 5)
    </video>
    
    
    <script> 
    $( "#video2" ).hide();
    function run(){
       $( "#video1" ).hide();
        $( "#video2" ).show();
        document.getElementById("video2").play();
       };
    </script> 
    
    0 讨论(0)
  • 2021-02-05 16:04

    You can't solve this issue in javascript. That delay you see depends on the video compression and the hardware.

    To start playing at a time that is not 0, the video decoder has to go back and find a key frame and then build the current frame by reading everything between the last key frame and your chosen time.

    I'm not an expert in video compression, but maybe there is a way to pick these key frames and place them exactly where you need them. I don't think it will be easy and smooth, though.


    If you're looking for an easier solution, use @Random's, but it uses two <video> tags to work around this limit.

    0 讨论(0)
  • 2021-02-05 16:05

    var iterations = 1;
    var flag = false;
    document.getElementById('iteration').innerText = iterations;
    var myVideo = document.getElementById('video-background');
    myVideo.addEventListener('ended', function() {
      alert('end');
      if (iterations < 2) {
        this.currentTime = 0;
        this.play();
        iterations++;
        document.getElementById('iteration').innerText = iterations;
      } else {
        flag = true;
        this.play();
      }
    }, false);
    myVideo.addEventListener('timeupdate', function() {
      if (flag == true) {
        console.log(this.currentTime);
        if (this.currentTime > 5.5) {
          console.log(this.currentTime);
          this.pause();
        }
      }
    }, false);
    <div>Iteration: <span id="iteration"></span></div>
    
    <video id="video-background" autoplay="" muted="" controls>
                <source src="https://res.cloudinary.com/video/upload/ac_none,q_60/bgvid.mp4" type="video/mp4">
        </video>
    <div>Iteration: <span id="iteration"></span></div>

    // Please note that loop attribute should not be there in video element in order for the 'ended' event to work in ie and firefox

    0 讨论(0)
提交回复
热议问题