Youtube API

From Wikicliki
Jump to: navigation, search

Insert Youtube Player into your Flash project

AS3

Flash AS3 API Reference for Youtube - The AS3 example on this main youtube site is for Flex. If you try to run the class in the example in Flash you will get an error cos of this line, since the Canvas Class is part of the Flex framework.

<highlightSyntax>import mx.containers.Canvas;</highlightSyntax>

Try using this instead if you are doing it in flash alone:

AS 2

  • Download basic_youtube_embed.fla from their site.
  • You will find the following code on the timeline:

<highlightSyntax> // create a MovieClip to load the player into var ytplayer:MovieClip = _root.createEmptyMovieClip("ytplayer", 1);

// create a listener object for the MovieClipLoader to use var ytPlayerLoaderListener:Object = {

 onLoadInit: function() {
   // When the player clip first loads, we start an interval to
   // check for when the player is ready
   loadInterval = setInterval(checkPlayerLoaded, 250);
 }

};

var loadInterval:Number; function checkPlayerLoaded():Void {

   // once the player is ready, we can subscribe to events, or in the case of
   // the chromeless player, we could load videos
   if (ytplayer.isPlayerLoaded()) {
       ytplayer.addEventListener("onStateChange", onPlayerStateChange);
       ytplayer.addEventListener("onError", onPlayerError);
       clearInterval(loadInterval);
   }

}

function onPlayerStateChange(newState:Number) {

   trace("New player state: "+ newState);

}

function onPlayerError(errorCode:Number) {

   trace("An error occurred: "+ errorCode);

}

// create a MovieClipLoader to handle the loading of the player var ytPlayerLoader:MovieClipLoader = new MovieClipLoader(); ytPlayerLoader.addListener(ytPlayerLoaderListener);

// load the player ytPlayerLoader.loadClip("http://www.youtube.com/v/gulD4VPtRAA", ytplayer); </highlightSyntax>

  • Note that the last line has gulD4VPtRAA - you should replace this with the Video ID. Your normal url may look like "http://www.youtube.com/watch?v=gulD4VPtRAA" - but you only need the ID at the end which v is equal to. In this example it might be gulD4VPtRAA.
  • unlisted youtube videos may require you to use the url with https instead of http

Flash AS2 API Reference for Youtube

See also