English | Site Directory

YouTube APIs and Tools

YouTube ActionScript 2.0 Player API Reference

This document provides reference information for the YouTube ActionScript 2.0 player API.

Contents

Overview

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.

Requirements

The end user must have Flash Player 8 or higher installed to view everything correctly.

Operations

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():Boolean
Returns true when the player SWF is loaded, initialized, and ready to receive API calls. It will return false or could be undefined before the player is completely loaded and initialized.
player.addEventListener(event:String, handler:Function)
This method behaves exactly the same as when using the JavaScript API, but accepts a function to handle the event as opposed to a string. See the Events section of the JavaScript API documentation for events that may be subscribed to.
player.destroy():Void
Destroys the player instance. This method should be called before unloading the player SWF from your parent SWF.

Examples

Loading the player SWF

Because 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.

Caveats

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.

Back to top