Link to home
Start Free TrialLog in
Avatar of creceveur
creceveurFlag for United States of America

asked on

Stop YouTube video auto-play onClick

I have a YouTube video that autorun's on a landing page.  At the bottom of the page, I have another YouTube video that the user can click on to play.  Upon clicking on the second video, I want the first one to stop playing so the two audio outputs don't mix and become indistinguishable.  Is this possible?  Thanks
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

You could have an image instead:
<script>
var yt1Object = '<object.....</object>'
var yt2Object = '<object.....</object>'
var yt1Image = '<a href="#" onClick="return swap('yt1','yt2')"><img src="yt1.jpg" border="0" /></a>'
var yt2Image = '<a href="#" onClick="return swap('yt2','yt1')"><img src="yt2.jpg" border="0" /></a>'
 
function swap(o1,o2) {
  document.getElementById(o2).innerHTML=eval(o2+'Image');
  document.getElementById(o1).innerHTML=eval(o1+'Object');
  return false;
} 
</script>
<div id="yt1"><object....></object></div>
<div id="yt2"><a href="#" onClick="return swap('yt2','yt1')"><img src="yt2.jpg" border="0" /></a></div>

Open in new window

Avatar of creceveur

ASKER

That would require a double-click to play the video - one to initialize the object, and another to play it.  That would also require creation of my of thumbnails, correct?

Is there not any way to monitor for clicks, and then stop the first video?  I found this code on the internet, but I don't understand Javascript enough to customize it to my page.

[code]function onClick(evt:MouseEvent):void {
  myPlayer.myVid.stop();
  gotoAndStop(evt.target.name.toString());
}

project.addEventListener(MouseEvent.CLICK, onClick);
anotherButton.addEventListener(MouseEvent.CLICK, onClick);[/code]
Also, that method would not show the current video playing, as it would load the other video's thumbnail, right?
The code I gave you will autoplay the top video as before if you cut and paste the object code you have into the div and script.

Yes you need a picture of the video or just a big play button the size of the video.

The code you pasted may or may not work depending on the plugin used to play the video.
The code I'm using for the video is (YouTube):

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/eQ8ZWMlObGE&hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/eQ8ZWMlObGE&hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344"></embed></object>

Is there a way to make the onclick script I posted work?
Thanks
I think the code you show is to add functionality to a flash embedded clip.

OK, this will work for me.  Is it possible to grow the script to swap between 3 videos?  Thanks
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thatelvis
thatelvis

Brilliant question brilliant reply

thanks all I spent days trying to get this done myself before checking here.

k