HTML5 Video to play after AJAX page load

前端 未结 4 1663
轮回少年
轮回少年 2021-01-21 07:58

I am struggling to get an HTML5 video to play when arriving at the page via an AJAX request.

If you refresh the page, or land directly on the page, it works fine. But wh

4条回答
  •  粉色の甜心
    2021-01-21 08:07

    For anyone struggling with the same issue, I found that after the ajax call the video had the property 'paused: true' even thought autoplay was set and I was calling video.play() on 'loadeddata'.

    The solution was to trigger video.play() when pause is detected. I also found that it worked smoother not having the 'autoplay' attribute on the video and became jerky after multiple initialisations.

    DOM:

    
    

    JS:

    video = jQuery('#video').get()[0];
    
    video.addEventListener('loadeddata', function() {
        video.play();
    });
    
    video.addEventListener('pause', function() {
        video.play();
    });
    

    Also, for anyone wondering why I might want this ability, it is for a video playing on the background of a webpage, hence no need for user to play/pause it.

提交回复
热议问题