简体   繁体   English

限制HTML5视频循环的数量

[英]Limit the Number of HTML5 Video Loops

:) I am trying to loop a HTML5 video for 3 times only. :)我试图将HTML5视频仅循环播放3次。 I've found this code snippet which would basically do what I want. 我发现此代码片段基本上可以满足我的要求。 The strange thing is, I can't get it to work. 奇怪的是,我无法正常工作。 If I run the demo here on stackoverflow it works. 如果我在stackoverflow上运行演示,它将起作用。 If I copy and paste it into a blank html document, it doesn't. 如果我将其复制并粘贴到空白的html文档中,则不会。 Any advice? 有什么建议吗?

thanks! 谢谢!

Do you add JS script at the end of body section? 您是否在主体部分的末尾添加了JS脚本? Script should be added at the end of body section (when document was loaded). 脚本应添加到正文部分的末尾(加载文档时)。

Try copy + pasting this. 尝试复制+粘贴此内容。 This should work. 这应该工作。

<div>Iteration: <span id="iteration"></span></div>

<video width="320" height="240" id="myVideo" controls>
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
    Your browser does not support the video tag.
</video>

<script>
    var iterations = 1;

    document.getElementById('iteration').innerText = iterations;

    myVideo.addEventListener('ended', function () {    

        if (iterations < 2) {
            this.currentTime = 0;
            this.play();
            iterations ++;          
            document.getElementById('iteration').innerText = iterations;
        }   

    }, false);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM