开发者

Looping HTML5 video,

I'm trying to loop a section of HTML video using jQuery, in particular, when the video reaches 7 seconds, I want it to loop back to 0. I can't cut the actual video to seven seconds and use the 'loop' function within the video tag as firefox and mobile safari don'开发者_如何学Ct support it.

The reason i need it to loop back to 0 is because we're streaming the looped video to Apple TV via AirPlay. I can get it to loop on the iPhone (Mobile Safari) but when it reaches the 'end' of the video, AirPlay stops. I want to trick the browser into playing indefinitely by never reaching the end.

Any Ideas?

<script type="text/javascript"> // Script to loop back to back to 0 when it reaches 7 seconds </script>

...

<video id="myVideo" controls="controls" loop="loop"><source src="video.m4v" type="video/mp4" x-webkit-airplay="allow" /></video>


<script type="text/javascript">
    $("#myVideo").bind("timeupdate", function(){
        if(this.currentTime > 7)
            this.currentTime = 0;
    })
</script>


Here is a slight alternative... https://stackoverflow.com/a/25722747/3681899

Important: Be careful with large files as it's more likely to pause.

<video id="video1" width="320" height="240" poster="movie.png" autoplay loop>
  <!--<source src="movie.webm" type="video/webm">-->
  <source src="http://www.w3schools.com/tags/movie.ogg" type="video/ogg">
  <source src="http://www.w3schools.com/tags/movie.mp4" type="video/mp4">
  <object data="http://www.w3schools.com/tags/movie.mp4" width="320" height="240">
    <!--<embed width="320" height="240" src="movie.swf">-->
  </object>
Optional test to be displayed if the browser doesn't support the video tag (HTML 5)
</video>

<script> 
(function () {
    var vidElem = document.getElementById('video1');
    console.log(vidElem);
    vidElem.addEventListener("timeupdate", function () {
        if (vidElem.currentTime > 7) {
            vidElem.currentTime = 0;
            vidElem.play();
        }
    }, false);
})();
</script> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜