We saw a very exciting YouTube launch for developers today. People have long wanted to customize the YouTube player, and now you have complete control with a chromeless player that has JavaScript access so you can start, stop, go to a timing, and much more.

I took this API and implemented a simple DSL that allows me to build a simple transcript table of contents that lets you jump to a particular chapter in the video.

The HTML has special attributes and CSS classes, ending up like this:

HTML:

  1.  
  2. <div id=”ytplayer” class=”ytplayer” url=”http://www.youtube.com/v/2SgMHjmZO60″></div>
  3.  
  4. <div id=”ytranscript” class=”ytranscript” for=”ytplayer”>
  5. <b>Click on the item you want to skip too</b>
  6. <li starttime=”0.0″>Hand, hand, fingers, thumb</li>
  7. <li starttime=”8.0″>Drumming and drumming</li>
  8. <li starttime=”28.0″>Time to pick the apples and the plums</li>
  9. <li starttime=”42.0″>Enter Jake</li>
  10. <li starttime=”44.0″>Enter Jack</li>
  11. <li starttime=”51.0″>The monkeys say bye bye</li>
  12.  
  13. <li starttime=”60.0″>Now they play bangos and fiddles</li>
  14. <li starttime=”78.0″>Wooah, millions of monkeys!</li>
  15. </ul>
  16. </div>
  17.  

My JavaScript trivially attaches behaviour to the list to talk to the player. It adds timing information from the DSL to the HTML content, and uses seekTo to get the player to the point you need.

JAVASCRIPT:

  1.  
  2. window.onload = function() {
  3.   $$(’.ytranscript’).each(function(e) {
  4.     var player = e.readAttribute(”for”);
  5.     var playerEmbed = player + “Embed”;
  6.     initPlayer(player, playerEmbed);
  7.    
  8.     var lis = e.getElementsByTagName(”li”);
  9.     var odd = 1;
  10.     $A(lis).each(function(li) {
  11.       var starttime = li.readAttribute(’starttime’);
  12.       li.innerHTML = li.innerHTML + ” <span class=’timing’>” + parseInt(starttime) + ” secs</span>”;
  13.       li.addClassName( (odd++ % 2) ? “odd” : “even”);
  14.       li.writeAttribute(”title”, “Send the player to this location”);
  15.       li.onclick = function() {
  16.         makeSureVideoIsPlaying($(playerEmbed));
  17.         $(playerEmbed).seekTo(starttime, true);
  18.       };
  19.     });
  20.   });
  21. }
  22.  
  23. function makeSureVideoIsPlaying(playerEmbed) {
  24.   if (playerEmbed.getState && $(playerEmbed).getState() == -1) { // -1 unstarted
  25.     playerEmbed.playVideo(); // play if we haven’t started
  26.   }
  27. }
  28.  
  29. function initPlayer(player, playerEmbed) {
  30.   var url = $(player).readAttribute(’url’);
  31.   var so = new SWFObject(url + “&enablejsapi=1&playerapiid=my” + playerEmbed, playerEmbed, 432, 400, 8);
  32.   so.addParam(”AllowScriptAccess”, “always”);
  33.   so.write(player);
  34. }
  35.  
  36. function onYouTubePlayerReady(playerId) {}
  37.  

The one gotcha is that seekTo goes to the nearest keyframe, which can be a few seconds off. Hopefully it will at least go to the nearest one before the timing, but that isn’t the case right now.

Learn more

We have an interview with the engineers, and a bunch of documentation.

Check out the JavaScript API and chromeless player reference to find out more.

Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/250165943/ytranscript-using-the-brand-new-youtube-chromeless-scriptable-player

Comments are closed.



Site Navigation