Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start/play embedded (iframe) youtube-video on click of an image

I'm trying to start playing an embedded youtube video by clicking an image. The idea is to have an image on top of a video, and when the image is clicked it fades out and starts playing the video.

I'm using jquery to fade the image, and was hoping to find a way to play or click the video using jquery as well.

The fading is working fine, but I can't figure out how to trigger the video to play. I got it to work on a couple of browsers by setting the video to autoplay and hide it, and then fade in the video when the image was clicked. On most browsers the video would autoplay when faded in, but in chrome it started to autoplay even when it was hidden. It didn't work well in iOS either.

Since I'm pretty new at this, I'm not even sure if I'm writing it 100% correct, but I've tried something like this without success:

     $('#IMAGE').click(function() { $('#VIDEO').play(); }); 

So, how would I go about to make the video play on an image click? Is there a way to play the video when the image is clicked, just using jquery?

Thank you in advance.

like image 563
Corn Avatar asked Jun 06 '11 00:06

Corn


People also ask

How do I autoplay a video in iframe?

Allowing iframes to autoplay video content You should also note the need to use the allowfullscreen attribute in complement to the allow attribute in order to support browsers that do not support allow attribute. If you are using amp-iframe and autoplay you need to add allow="autoplay" to your amp-iframe element.

How do you embed a YouTube video at a specific start and end time?

To have an embedded YouTube video begin playing at a specific timestamp, first calculate the start point in total number of seconds (60 times the number of minutes plus the number of seconds), then do the same for the end point. Grab the embed code from YouTube, by clicking on Share > Embed.

How do you embed a YouTube video into a picture?

You'll need to be using YouTube on a computer to get the embed code for your video. Go to the video you want to embed. Search for the video, and then click its name in the search results. Decide where you want the embedded video to begin.


2 Answers

The quick and dirty way is to simply swap out the iframe with one that has autoplay=1 set using jQuery.

THE HTML

Placeholder:

<div id="videoContainer">   <iframe width="450" height="283" src="https://www.youtube.com/embed/VIDEO_ID_HERE?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe> </div> 

Autoplay link:

<a class="introVid" href="#video">Watch the video</a></p> 


THE JQUERY

The onClick catcher that calls the function

jQuery('a.introVid').click(function(){   autoPlayVideo('VIDEO_ID_HERE','450','283'); }); 

The function

/*--------------------------------   Swap video with autoplay video ---------------------------------*/  function autoPlayVideo(vcode, width, height){   "use strict";   $("#videoContainer").html('<iframe width="'+width+'" height="'+height+'" src="https://www.youtube.com/embed/'+vcode+'?autoplay=1&loop=1&rel=0&wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe>'); } 
like image 146
gearsandcode Avatar answered Sep 21 '22 18:09

gearsandcode


You can do this simply like this

$('#image_id').click(function() {   $("#some_id iframe").attr('src', $("#some_id iframe", parent).attr('src') + '?autoplay=1');  }); 

where image_id is your image id you are clicking and some_id is id of div in which iframe is also you can use iframe id directly.

like image 27
drupal_mohitsharma Avatar answered Sep 19 '22 18:09

drupal_mohitsharma