This document provides reference information for the YouTube ActionScript 2.0 player API.
The ActionScript 2.0 API allows users to control the YouTube video players by loading the player into a Flash application and making calls via ActionScript to the player's public API. Calls can be made to play, pause, seek to a certain time in a video, set the volume, mute the player, and other useful functions. The ActionScript API is automatically activated when the player is loaded into another SWF.
The end user must have Flash Player 8 or higher installed to view everything correctly.
The ActionScript API is the same as the JavaScript API, with some small changes to how the player is initialized. For a list of available API calls, see the JavaScript API documentation. For examples of how to initialize and make calls to the player via ActionScript, see the examples below.
The ActionScript specific API calls are listed below:
player.isPlayerLoaded():Booleanplayer.addEventListener(event:String, handler:Function)player.destroy():VoidBecause of the architecture of the player SWF, using ActionScript's built in MovieClipLoader will not give you accurate information. To detect when the player SWF is ready to receive API calls, you should call player.isPlayerLoaded(), which will return true when the player is completely loaded and initialized.
At this point, you may subscribe to events and make any other API calls to the player.
In this example, we wait for the player SWF to load using MovieClipLoader's onLoadInit event, and then start an interval to check for when the player SWF is initialized.
// create a MovieClip to load the player into
ytplayer = createEmptyMovieClip("ytplayer", 1);
// create a listener object for the MovieClipLoader to use
ytPlayerLoaderListener = {};
var loadInterval:Number;
// When the player clip first loads, we start an interval to check for when the
// player is ready
ytPlayerLoaderListener.onLoadInit = function() {
loadInterval = setInterval(checkPlayerLoaded, 250);
}
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);
loadIndicator._visible = false;
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
ytPlayerLoader = new MovieClipLoader();
ytPlayerLoader.addListener(ytPlayerLoaderListener);
// load the player
ytPlayerLoader.loadClip("http://www.youtube.com/v/VIDEO_ID", ytplayer);
Once the player is loaded and ready, all API calls can be made in the same way as the JavaScript API.
When loading multiple player SWFs, (including loading a new player SWF in place of a previous player) you will need to load the player into a MovieClip with a different name than previous players. For example, if you loaded a video into _root.youtubeplayer, when you want to load a new player, you would use something like _root.youtubeplayer2, and so on.
When unloading a YouTube player, you should always call
destroy() first. This will close the NetStream object and stop the
video from continuing to download even after the player has been unloaded. It
will also remove references to the player SWF so if you load a new player SWF
into your application, the new SWF can load and initialize properly.