views:

396

answers:

3

Hi, I'm embedding YouTube videos onto my webpage with something like this

<object width="425" height="344">
    <param name="movie" value="http://www.youtube.com/v/RU-bMtPz1cY"&gt;&lt;/param&gt;
    <param name="allowFullScreen" value="false"></param>
    <param name="allowscriptaccess" value="always"></param>
    <embed src="http://www.youtube.com/v/RU-bMtPz1cY" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
</object>

If I have say 3 YouTube videos on the same webpage, I was wondering if it was possible (maybe with YouTube's API?) to play each video consecutively? That is, the first video starts to autoplay. When that video finishes, the second video on the webpage will play and then the third? Thanks for your time.

Reference: http://code.google.com/apis/youtube/js_api_reference.html

+1  A: 

You're going to want to create a playlist containing the videos you want to play and then embed that into your site. Direct instructions can be found here.

Shaun Hamman
Is there any way to do this without playlists? The videos are dynamic on the page. I was looking instead for some type of javascript event to play the video and detect when one finishes
Axsuul
+2  A: 

See http://code.google.com/apis/youtube/js_api_reference.html#Events

You want to listen to the onStateChange event. note that when the value passed is 0, then the video has ended. Listen for that event/value to start the next video.

Just a basic outline of what you probably want to do:

  1. add end listener for video
  2. play video
  3. when end event fired (and caught), go to next video and repeat from step 1.
Jonathan Fingland
+1  A: 

You could use a jQuery plugin -- the jQuery YouTube TubePlayer Plugin

provided you have the right markup, (in this example - an an 'active' class on the video thats currently playing) you could basically set something up like..

jQuery("#player-container").tubeplayer({
      onPlayerEnded:function(){
            var $vid = jQuery(".active").next();
            if($vid.length){
                  jQuery('#player-container').tubeplayer('play',vid.attr("vid");
                  $vid.siblings().andSelf().removeClass("active");
                  $vid.addClass("active");
            }
      }
});

Nirvana Tikku