This document provides reference information for the YouTube ActionScript 2.0 player API.
![]() |
Note: YouTube now supports ActionScript 3.0 in addition to ActionScript 2.0. We recommend that you use the ActionScript 3.0 Player API for any new applications. We also recommend that you migrate existing applications that use the ActionScript 2.0 Player API to use ActionScript 3.0 instead. |
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()
, loadVideoById()
, cueVideoByUrl()
or loadVideoByUrl()
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.
Queueing functions
player.cueVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):Void
playVideo()
or seekTo()
is called.
videoId
parameter specifies the YouTube Video ID of the video to be played. In YouTube Data API video feeds, the <yt:videoId>
tag specifies the ID.startSeconds
parameter accepts a float/integer and specifies the time from which the video should start playing when playVideo()
is called. If you specify a startSeconds
value and then call seekTo()
, then 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).suggestedQuality
parameter specifies the suggested playback quality for the video. Please see the definition of the setPlaybackQuality
function for more information about playback quality.player.loadVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):Void
videoId
parameter specifies the YouTube Video ID of the video to be played. In YouTube Data API video feeds, the <yt:videoId>
tag specifies the ID.startSeconds
parameter accepts a float/integer. If it is specified, then the video will start from the closest keyframe to the specified time.suggestedQuality
parameter specifies the suggested playback quality for the video. Please see the definition of the setPlaybackQuality
function for more information about playback quality.player.cueVideoByUrl(mediaContentUrl:String, startSeconds:Number):Void
playVideo()
or seekTo()
is called.
mediaContentUrl
must be a fully qualified YouTube player URL in the format http://www.youtube.com/v/VIDEO_ID
. In YouTube Data API video feeds, the url
attribute of the <media:content>
tag contains a fully qualified player URL when the tag's format
attribute has a value of 5
.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()
, then 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.loadVideoByUrl(mediaContentUrl:String, startSeconds:Number):Void
mediaContentUrl
must be a fully qualified YouTube player URL in the format http://www.youtube.com/v/VIDEO_ID
. In YouTube Data API video feeds, the url
attribute of the <media:content>
tag contains a fully qualified player URL when the tag's format
attribute has a value of 5
.startSeconds
accepts a float/integer and specifies the time from which the video should start playing. If startSeconds
(number can be a float) is specified, the video will start from the closest keyframe to the specified time.Playback controls and player settings
Playing a video
player.playVideo():Void
player.pauseVideo():Void
player.stopVideo():Void
player.seekTo(seconds:Number, allowSeekAhead:Boolean):Void
seekTo()
function 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.allowSeekAhead
parameter determines whether or not the player will make a new request to the server if seconds
is beyond the currently loaded video data.player.clearVideo():Void
stopVideo()
. Note that this function has been deprecated in the ActionScript 3.0 Player API.Changing the player volume
player.mute():Void
player.unMute():Void
player.isMuted():Boolean
player.setVolume(volume:Number):Void
player.getVolume():Number
getVolume()
will return the volume even if the player is muted.Setting the player size
player.setSize(width:Number, height:Number):Void
Playback status
player.getVideoBytesLoaded():Number
player.getVideoBytesTotal():Number
player.getVideoStartBytes():Number
player.getPlayerState():Number
player.getCurrentTime():Number
Playback quality
player.getPlaybackQuality():String
undefined
if there is no current video. Possible return values are hd720
, large
, medium
and small
.player.setPlaybackQuality(suggestedQuality:String):Void
onPlaybackQualityChange
event will fire, and your code should respond to the event rather than the fact that it called the setPlaybackQuality
function.suggestedQuality
parameter value can be small
, medium
, large
, hd720
or default
. Setting the parameter value to default
instructs YouTube to select the most appropriate playback quality, which will vary for different users, videos, systems and other playback conditions.medium
quality video will actually look better than a large
quality video. The following list shows recommended playback quality levels for different player sizes:small
: Player resolution less than 640px by 360px.medium
: Minimum player resolution of 640px by 360px.large
: Minimum player resolution of 854px by 480px.hd720
: Minimum player resolution of 1280px by 720px.default
: YouTube selects the appropriate playback quality. This setting effectively reverts the quality level to the default state and nullifies any previous efforts to set playback quality using the cueVideoById
, loadVideoById
or setPlaybackQuality
functions.setPlaybackQuality
function with a suggestedQuality
level that is not available for the video, then the quality will be set to the next lowest level that is available. For example, if you request a quality level of large
, and that is unavailable, then the playback quality will be set to medium
(as long as that quality level is available).suggestedQuality
to a value that is not a recognized quality level is equivalent to setting suggestedQuality
to default
.player.getAvailableQualityLevels():Array
hd720
, large
, medium
and small
. This function returns an empty array if there is no current video.Retrieving video information
player.getDuration():Number
getDuration()
will return 0 until the video's metadata is loaded, which normally happens just after the video starts playing.player.getVideoUrl():String
player.getVideoEmbedCode():String
Adding an event listener
player.addEventListener(event:String, listener:Function):Void
event
. The Events section below identifies the different events that the player might fire. The listener
is a reference to the function that will execute when the specified event fires.The ActionScript specific API calls are listed below:
player.isPlayerLoaded():Boolean
player.destroy():Void
onStateChange
onPlaybackQualityChange
setPlaybackQuality(suggestedQuality)
function, this event will fire if the playback quality actually changes. Your code should respond to the event and should not assume that the quality will automatically change when the setPlaybackQuality(suggestedQuality)
function is called. Similarly, your code should not assume that playback quality will only change as a result of an explicit call to setPlaybackQuality
or any other function that allows you to set a suggested playback quality.onError
100
, 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.
The sample code below demonstrates how YouTube determines the status of its quality toggle button, which lets users select a different playback quality based on available quality levels for a video.
var qualityLevels:Array = getAvailableQualityLevels() // qualityLevels may be undefined if the API function does not exist, // in which case this conditional is false if (qualityLevels.length > 1) { var highestQuality:String = qualityLevels[0] if (highestQuality begins with "hd") { // Brand quality toggle button as HD. } else { // Brand quality toggle button as HQ. // The highest available quality is shown, but it is not HD video. } var quality:String = getPlaybackQuality(); if (quality == 'small' || quality == 'medium') { // The user is not watching the highest quality available // can can toggle to a higher quality. } else { // The user is watching the highest quality available // and can toggle to a lower quality. } } else { // Hide the toggle button because there is no current video or // there is only one quality available. }
Please refer to the instructions for using ActionScript 3.0 for more information.
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()
.