How to play/start youtube video after clicking on an image?

删除回忆录丶 提交于 2019-11-30 06:40:46
Laurent Zuijdwijk

Have a look at:

Youtube API examples

the code will be something like this:

function onPlayerReady(event) {
    $('img').click(function() { 
        ytPlayer.playVideo();
    });
}
Shahadat

.click() JQuery event handler:

$("#clickMe").click(function(){
  $("#iframe")[0].src += "&autoplay=1";
  ev.preventDefault();
});

Another approach - switch iframe SRC (URL) with "?autoplay=1" version.

<a href="#" id="clickMe">PLAY</a>

<iframe id="iframe" width="1280" height="720" src="//www.youtube.com/embed/F0naUkyEkmM" 
            data-autoplay-src="//www.youtube.com/embed/F0naUkyEkmM?autoplay=1"></iframe>

jQuery:

jQuery("#clickMe").on('click',function(){
    var vi = jQuery("#iframe");
    vi.attr("src", vi.data("autoplay-src") );
});

I think this will help you. Working fine for me.

$('.trigger').on('click', function () {
    $(".video")[0].src += "1";
});
<a href="#" class="trigger">Click</a>
        
<iframe class="video" src="https://www.youtube.com/embed/HJX71gFz_do?autoplay=" frameborder="0" allowfullscreen></iframe>

adding autoplay=1 only seems to work the first time. I am using a start/stop button and when i click it the first time, i add autoplay=1. Then when i click it again I remove the autoplay=1. That works fine.

but when i try to start it again (adding autoplay=1), it won't start.

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