JQuery autoplay video

前端 未结 3 1533
盖世英雄少女心
盖世英雄少女心 2020-12-17 07:09

Hi I have some very basic html for a video:

3条回答
  •  我在风中等你
    2020-12-17 07:53

    You can use:

    $("#video_player")[0].play();
    

    Or:

    $("#video_player")[0].autoplay = true;
    

    Or:

    document.getElementById("video_player").setAttribute('autoplay', true);
    

    Whatever suits you. Using $('#video_player').play(); you are referencing to non-existing method play in jQuery and you should reference to the object found by $("#video_player").

    To change the src of the video in JS, you just simply need something like that:

    function playFile() {
        var video = $("#video_player");
        video[0].src = "http://www.yoursrc.com/video_new.mp4";
        video[0].load();
        video[0].play();
    };
    


    NOTE:
    In Chrome, you should also need to add muted property if you want to use the autoplay attribute. Refer to : https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

提交回复
热议问题