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.
To add the embedded player for a specific video into your Flash file, you will be loading the following SWF into a MovieClip:
http://www.youtube.com/v/VIDEO_ID
Alternatively, you may wish to load the chromeless player into your MovieClip if you are building your own custom controls in Flash:
http://www.youtube.com/apiplayer
Once the chromeless player SWF has been loaded, you can use cueVideoById() or loadVideoById() to load a particular YouTube video.
See the examples below for more detailed information about how to embed a YouTube player SWF into your Flash file.
The ActionScript API is very similar to the JavaScript API, with some small changes to how the player is initialized and some additional functions. For examples of how to initialize and make calls to the player via ActionScript, see the examples below.
player.playVideo():Voidplayer.pauseVideo():Voidplayer.stopVideo():VoidstopVideo() is called, a video cannot be resumed without reloading the player or loading a new video (chromeless player only). When calling stopVideo(), the player broadcasts an end event (0).player.clearVideo():VoidstopVideo().player.getVideoBytesLoaded():Numberplayer.getVideoBytesTotal():Numberplayer.getVideoStartBytes():Numberplayer.mute():Voidplayer.unMute():Voidplayer.isMuted():Booleanplayer.setVolume(volume):Voidplayer.getVolume():NumbergetVolume() will return the volume even if the player is muted.player.seekTo(seconds, allowSeekAhead):VoidallowSeekAhead determines whether or not the player will make a new request to the server if seconds is beyond the currently loaded video data. Note that seekTo() will look for the closest keyframe before the seconds specified. This means that sometimes the play head may seek to just before the requested time, usually no more than ~2 seconds.player.getPlayerState():Numberplayer.getCurrentTime():Numberplayer.getDuration():NumbergetDuration() will return 0 until the video's metadata is loaded, which normally happens just after the video starts playing.player.addEventListener(event:String, listener):Voidplayer.getVideoUrl():Stringplayer.getVideoEmbedCode():Stringplayer.loadVideoById(videoId:String, startSeconds:Number):VoidstartSeconds (number can be a float) is specified, the video will start from the closest keyframe to the specified time.player.cueVideoById(videoId:String, startSeconds:Number):VoidplayVideo() or seekTo() is called. startSeconds accepts a float/integer and specifies the time from which the video should start playing when playVideo() is called. If you specify startSeconds and then call seekTo(), the startSeconds is value is ignored and the player plays from the time specified in the seekTo() call. When the video is cued and ready to play, the player will broadcast a video cued event (5).player.setSize(width:Number, height:Number):VoidThe ActionScript specific API calls are listed below:
player.isPlayerLoaded():Booleanplayer.destroy():VoidonStateChangeonError100, 101, and 150. The 100 error code is broadcast when the video requested is not found. This occurs when a video has been removed (for any reason), or it has been marked as private. The 101 error code is broadcast when the video requested does not allow playback in the embedded players. The error code 150 is the same as 101, it's just 101 in disguise!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.
This is the easiest way to get started if you have Adobe Flash installed. Just open the FLA and publish the SWF. The code can be seen in the "Actions - Frame" tab.
If you don't have Flash installed you can compile a simple AS2 file that includes the player. This can then be compiled with an ActionScript compiler like MTASC.
To compile the above AS2 source file into a SWF, use the following MTASC command:
path/to/mtasc -swf ytdemo.swf -main -header 800:600:20 YTDemo.as
Once the player is loaded and ready, all API calls can be made in the same way as the JavaScript API.
Currently, all of the Player API SWFs are only available in AS2. To use Player API in AS3, you must wrap the API SWF in an AS2 wrapper that provides an interface to AS3 using LocalConnection or ExternalInterface.
TubeLoc is an ActionScript 3 library that serves as an adapter to the YouTube ActionScript 2 Player API. Easily add YouTube video functionality to your ActionScript 3 or Flex app with either the full-chrome or the chromeless YouTube players.
There are other resources which you may find useful in your quest to get AS3 working correctly with YouTube.
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.
You should only call destroy() on the player. There is no need to call removeMovieClip().