Search code examples
javascripthtmlvideoimpress.js

Video not autoplaying inspite of using "autoplay" in the video tag of HTML


The code snippet is given below. I have used impress.js to create an online presentation. I want to insert a video in the last slide which autoplays when the slide comes up. But I am unable to implement that. Can anyone please help me with this?

<div class="step box" data-x="0" data-z="4000" data-rotate-y="0">

<video width="800" height="600" controls autoplay>
   <source src="electric_bulb_hd_stock_video.mp4" type="video/mp4">
    Your browser does not support HTML5 video.
</video>

</div>

Solution

  • In impress.js, the DOM prevent video to autoplay, but you can use API, like this:

    var videoStep = document.getElementById("video-step");
    var video = document.getElementById("video");
    videoStep.addEventListener("impress:stepenter", function(){
        video.play();
    }, false);
    videoStep.addEventListener("impress:stepleave", function(){
        video.pause();
    }, false);
    

    HTML:

    <div id="video-step" class="step" data-x="-50000" data-y="2000" data-z="-60000" data-scale="6">
        <video id="video" width="420" height="340" autoplay>
            <source src="video/test.webm" type="video/webm">
        </video>
    </div>
    

    With the code above, when you enter this step, the video will play automatically, and it will pause when you leave this step. Give it a try :)