Mobile Safari HTML5 video - event listener 'ended' does not fire the 2nd time

痞子三分冷 提交于 2019-12-05 07:03:52

I believe that the "ended" event no longer fires when a VIDEO element reaches the end. Apparently only the "pause" event fires.

I've gotten around this by simply listening to the "timeupdate" event and associating a handler method that checks if the currentTime property is equivalent to the duration property of the VIDEO element.

UPDATE: I sometimes see the "ended" event in iOS. I always see the "pause" event. Here's some jQuery code that displays this information in the browser console:

    (function ($) {
        $("#vid").bind("timeupdate", function (e) {
            console.log(e.type + " - " + (this.currentTime == this.duration));
        });
    })(jQuery);

This is due to a strange bug in Safari's HTML5 video tag implementation. It can be reproduced on Safari for Windows as well. I've just found one workaround for this problem - just bind to loadedmetadata event and set the currentTime to some non-zero value. Here is an example:

<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
</head>
<body>
<video id="video" width="500" height="400" controls autoplay></video>
<script>
var src = [
 "http://content.adfox.ru/131007/adfox/205544/865991_11.mp4",
 "http://all.rutube.ru/130627/gpmdigital/217059/805529_11.mp4"
];
var curSrc = 0;
$(function() {
 $('#video').attr("src", src[curSrc % src.length]);
 curSrc++;
 var video = $('#video').get(0);

 $('#video')
 .on('loadedmetadata', function() {
  video.currentTime=0.01;
  video.play();
 })
 .on('ended', function() {
  console.log('ended');
  video.src = src[curSrc % src.length];
  video.load();
  curSrc++;
 });
});
</script>
</body>
</html>

You can try this demo in this jsfiddle: http://jsfiddle.net/j2knz6sv/

Sami Yilmaz

You should use timeupdate like this:

this.currentTime >= (this.duration-1)

Otherwise, it doesn't fire an event.

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