简体   繁体   中英

HTML5 media events not working as expected

I want to create an image fallback in case a video couldn't be loaded. I found out that the solution is to use media events for this problem. However, they don't seem to work as expected.

/**
 * Dynamically load the video within the given slide.
 */
var loadVideo = function(slide) {
    var video = $('<video muted>'),
        source = $('<source>', {
            src: slide.attr('data-video'),
            type: 'video/mp4'
        });

    video.html(source);

    video[0].addEventListener('canplay', revealVideo(video), false);
    video[0].addEventListener('error', function() {
        alert('Video could not be loaded.');
        // fallback
    }, false);
    video[0].onended = function() { replayVideo(video) };

    slide.append(video);
};

It doesn't matter whether the video is succesfully loaded or not, revealVideo() will always get called. The eventListener error is never triggered (for instance when using a src which doesn't exist).

I found the solution. It appears that the <source> element is recieving errors, not the video element itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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