Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an image and I want to start a youtube video only after I click the image. How can I do this?

Thanks a lot!

like image 648
Grozav Alex Ioan Avatar asked Sep 13 '11 17:09

Grozav Alex Ioan


3 Answers

Have a look at:

Youtube API examples

the code will be something like this:

function onPlayerReady(event) {
    $('img').click(function() { 
        ytPlayer.playVideo();
    });
}
like image 83
Laurent Zuijdwijk Avatar answered Oct 14 '22 19:10

Laurent Zuijdwijk


.click() JQuery event handler:

$("#clickMe").click(function(){
  $("#iframe")[0].src += "&autoplay=1";
  ev.preventDefault();
});
like image 23
Shahadat Avatar answered Oct 14 '22 20:10

Shahadat


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") );
});
like image 7
Marek Avatar answered Oct 14 '22 21:10

Marek