This document provides reference information for the YouTube JavaScript player API.
Contents
Overview
The JavaScript API allows users to control the YouTube chromeless or embedded video players via JavaScript. 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.
Requirements
The end user must have Flash Player 10.1 or higher installed to view everything correctly. Because of this requirement, we suggest using SWFObject to embed the SWF and detect the user's Flash Player version.
In addition, any HTML page that contains the YouTube player must implement a JavaScript function named onYouTubePlayerReady. The API will call this function when the player is fully loaded and the API is ready to receive calls. See the Event Handlers section for more details.
Embedded players must have a viewport that is at least 200px by 200px. If the player displays controls, it must be large enough to fully display the controls without shrinking the viewport below the minimum size. We recommend 16:9 players be at least 480 pixels wide and 270 pixels tall.
Getting Started
Note: To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet.
You can enable the JavaScript API handlers for chromeless and embedded players. To enable the JavaScript API for a player, add the URL parameter enablejsapi=1 to the player's URL.
-
Enabling the JavaScript API for a chromeless player
If your application is using a chromeless player, use the following URL to load the player in your application and enable JavaScript API handlers in the player. You can then build your own custom player controls using JavaScript:
http://www.youtube.com/apiplayer?enablejsapi=1&version;=3
-
Enabling the JavaScript API for an embedded player
Use the following URL to load an embedded video player. In the URL, replace the string VIDEO_ID with the 11-character YouTube video ID that identifies the video that the player will show.
http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1
When the player is ready, the API will call the onYouTubePlayerReady callback function. You can use the optional playerapiid parameter to the player URL to pass a value, such as the player ID, back to the callback function.
http://www.youtube.com/v/VIDEO_ID?enablejsapi=1&version=3&playerapiid=ytplayer
Once the player SWF is loaded, you can use the queueing functions – cueVideoById(), loadVideoById(), cueVideoByUrl(), loadVideoByUrl(), cuePlaylist(), or loadPlaylist() – to load a particular YouTube video or playlist.
The Player API demo lets you compare the chromeless and embedded video players, and the examples below provide more detailed information about how to embed a YouTube player SWF into your page. You can also customize your users' experience by using player parameters to control various types of player behavior.
Operations
In order to call the player API methods, you must first get a reference to the player object you wish to control. If you are using SWFObject to embed the player SWF, you can obtain the reference to the player object by calling getElementById() on the <object> or <embed> tag that contains the player SWF.
Functions
The following subsections list the functions that the player API supports.
-
The argument syntax requires function arguments to be listed in a prescribed order.
-
The object syntax lets you pass an object as a single parameter and to define object properties for the function arguments that you wish to set. In addition, the API may support additional functionality that the argument syntax does not support.
-
Argument syntax
loadVideoById("bHQqvYy5KYo", 5, "large") -
Object syntax
loadVideoById({'videoId': 'bHQqvYy5KYo', 'startSeconds': 5, 'endSeconds': 60, 'suggestedQuality': 'large'}); cueVideoById-
-
Argument syntax
player.cueVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):Void -
Object syntax
player.cueVideoById({videoId:String, startSeconds:Number, endSeconds:Number, suggestedQuality:String}):Void
This function loads the specified video's thumbnail and prepares the player to play the video. The player does not request the FLV until
playVideo()orseekTo()is called.- The required
videoIdparameter specifies the YouTube Video ID of the video to be played. In YouTube Data API video feeds, the<yt:videoid>tag specifies the ID. - The optional
startSecondsparameter accepts a float/integer and specifies the time from which the video should start playing whenplayVideo()is called. If you specify astartSecondsvalue and then callseekTo(), then the player plays from the time specified in theseekTo()call. When the video is cued and ready to play, the player will broadcast avideo cuedevent (5). - The optional
endSecondsparameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing whenplayVideo()is called. If you specify anendSecondsvalue and then callseekTo(), theendSecondsvalue will no longer be in effect. - The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
loadVideoById-
-
Argument syntax
player.loadVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):Void -
Object syntax
player.loadVideoById({videoId:String, startSeconds:Number, endSeconds:Number, suggestedQuality:String}):Void
This function loads and plays the specified video.
- The required
videoIdparameter specifies the YouTube Video ID of the video to be played. In YouTube Data API video feeds, the<yt:videoid>tag specifies the ID. - The optional
startSecondsparameter accepts a float/integer. If it is specified, then the video will start from the closest keyframe to the specified time. - The optional
endSecondsparameter accepts a float/integer. If it is specified, then the video will stop playing at the specified time. - The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
cueVideoByUrl-
-
Argument syntax
player.cueVideoByUrl(mediaContentUrl:String, startSeconds:Number, suggestedQuality:String):Void -
Object syntax
player.cueVideoByUrl({mediaContentUrl:String, startSeconds:Number, endSeconds:Number, suggestedQuality:String}):Void
This function loads the specified video's thumbnail and prepares the player to play the video. The player does not request the FLV until
playVideo()orseekTo()is called.- The required
mediaContentUrlparameter specifies a fully qualified YouTube player URL in the formathttp://www.youtube.com/v/VIDEO_ID?version=3. In YouTube Data API video feeds, the<media:content>tag'surlattribute contains a fully qualified player URL when the tag'sformatattribute has a value of5. - The optional
startSecondsparameter accepts a float/integer and specifies the time from which the video should start playing whenplayVideo()is called. If you specifystartSecondsand then callseekTo(), then the player plays from the time specified in theseekTo()call. When the video is cued and ready to play, the player will broadcast avideo cuedevent (5). - The optional
endSecondsparameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing whenplayVideo()is called. If you specify anendSecondsvalue and then callseekTo(), theendSecondsvalue will no longer be in effect. - The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
loadVideoByUrl-
-
Argument syntax
player.loadVideoByUrl(mediaContentUrl:String, startSeconds:Number, suggestedQuality:String):Void -
Object syntax
player.loadVideoByUrl({mediaContentUrl:String, startSeconds:Number, endSeconds:Number, suggestedQuality:String}):Void
This function loads and plays the specified video.
- The required
mediaContentUrlparameter specifies a fully qualified YouTube player URL in the formathttp://www.youtube.com/v/VIDEO_ID?version=3. In YouTube Data API video feeds, theurlattribute of the<media:content>tag contains a fully qualified player URL when the tag'sformatattribute has a value of5. - The optional
startSecondsparameter accepts a float/integer and specifies the time from which the video should start playing. IfstartSeconds(number can be a float) is specified, the video will start from the closest keyframe to the specified time. - The optional
endSecondsparameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing. - The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
-
Argument syntax
player.cuePlaylist(playlist:String|Array, index:Number, startSeconds:Number, suggestedQuality:String):Void- Queues the specified playlist. When the playlist is cued and ready to play, the player will broadcast a
video cuedevent (5).-
The required
playlistparameter specifies an array of YouTube video IDs. In YouTube Data API feeds, the<yt:videoid>tag specifies a video ID. -
The optional
indexparameter specifies the index of the first video in the playlist that will play. The parameter uses a zero-based index, and the default parameter value is0, so the default behavior is to load and play the first video in the playlist. -
The optional
startSecondsparameter accepts a float/integer and specifies the time from which the first video in the playlist should start playing when theplayVideo()function is called. If you specify astartSecondsvalue and then callseekTo(), then the player plays from the time specified in theseekTo()call. If you cue a playlist and then call theplayVideoAt()function, the player will start playing at the beginning of the specified video. -
The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
player.loadPlaylist(playlist:String|Array, index:Number, startSeconds:Number, suggestedQuality:String):Void- This function loads the specified playlist and plays it.
-
The required
playlistparameter specifies an array of YouTube video IDs. In YouTube Data API feeds, the<yt:videoid>tag specifies a video ID. -
The optional
indexparameter specifies the index of the first video in the playlist that will play. The parameter uses a zero-based index, and the default parameter value is0, so the default behavior is to load and play the first video in the playlist. -
The optional
startSecondsparameter accepts a float/integer and specifies the time from which the first video in the playlist should start playing. -
The optional
suggestedQualityparameter specifies the suggested playback quality for the video. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
-
Object syntax
player.cuePlaylist({listType:String,
list:String,
index:Number,
startSeconds:Number,
suggestedQuality:String}):Void- Queues the specified list of videos. The list can be a playlist, a search results feed, or a user's uploaded videos feed. When the list is cued and ready to play, the player will broadcast a
video cuedevent (5).-
The optional
listTypeproperty specifies the type of results feed that you are retrieving. Valid values areplaylist,search, anduser_uploads. The default value isplaylist. -
The required
listproperty contains a key that identifies the particular list of videos that YouTube should return.- If the
listTypeproperty value isplaylist, then thelistproperty specifies the playlist ID or an array of video IDs. In YouTube Data API feeds, the<yt:playlistid>tag specifies a playlist ID, and the<yt:videoid>tag specifies a video ID. - If the
listTypeproperty value issearch, then thelistproperty specifies the search query. - If the
listTypeproperty value isuser_uploads, then thelistproperty identifies the user whose uploaded videos will be returned.
- If the
-
The optional
indexproperty specifies the index of the first video in the list that will play. The parameter uses a zero-based index, and the default parameter value is0, so the default behavior is to load and play the first video in the list. -
The optional
startSecondsproperty accepts a float/integer and specifies the time from which the first video in the list should start playing when theplayVideo()function is called. If you specify astartSecondsvalue and then callseekTo(), then the player plays from the time specified in theseekTo()call. If you cue a list and then call theplayVideoAt()function, the player will start playing at the beginning of the specified video. -
The optional
suggestedQualityproperty specifies the suggested playback quality for the list's videos. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
player.loadPlaylist({list:String,
listType:String,
index:Number,
startSeconds:Number,
suggestedQuality:String}):Void- This function loads the specified list and plays it. The list can be a playlist, a search results feed, or a user's uploaded videos feed.
-
The optional
listTypeproperty specifies the type of results feed that you are retrieving. Valid values areplaylist,search, anduser_uploads. The default value isplaylist. -
The required
listproperty contains a key that identifies the particular list of videos that YouTube should return.- If the
listTypeproperty value isplaylist, then thelistproperty specifies a playlist ID or an array of video IDs. In YouTube Data API feeds, the<yt:playlistid>tag specifies a playlist ID, and the<yt:videoid>tag specifies a video ID. - If the
listTypeproperty value issearch, then thelistproperty specifies the search query. - If the
listTypeproperty value isuser_uploads, then thelistproperty identifies the user whose uploaded videos will be returned.
- If the
-
The optional
indexproperty specifies the index of the first video in the list that will play. The parameter uses a zero-based index, and the default parameter value is0, so the default behavior is to load and play the first video in the list. -
The optional
startSecondsproperty accepts a float/integer and specifies the time from which the first video in the list should start playing. -
The optional
suggestedQualityproperty specifies the suggested playback quality for the list's videos. Please see the definition of thesetPlaybackQualityfunction for more information about playback quality.
-
player.playVideo():Void- Plays the currently cued/loaded video. The final player state after this function executes will be
playing(1).
Note: A playback only counts toward a video's official view count if it is initiated via a native play button in the player. player.pauseVideo():Void- Pauses the currently playing video. The final player state after this function executes will be
paused(2) unless the player is in theended(0) state when the function is called, in which case the player state will not change. player.stopVideo():Void- Stops and cancels loading of the current video. This function should be reserved for rare situations when you know that the user will not be watching additional video in the player. If your intent is to pause the video, you should just call the
pauseVideofunction. If you want to change the video that the player is playing, you can call one of the queueing functions without callingstopVideofirst.
Important: Unlike thepauseVideofunction, which leaves the player in thepaused(2) state, thestopVideofunction could put the player into any not-playing state, includingended(0),paused(2),video cued(5) orunstarted(-1). player.seekTo(seconds:Number, allowSeekAhead:Boolean):Void- Seeks to a specified time in the video. If the player is paused when the function is called, it will remain paused. If the function is called from another state (
playing,video cued, etc.), the player will play the video.-
The
secondsparameter identifies the time to which the player should advance.The player will advance to the closest keyframe before that time unless the player has already downloaded the portion of the video to which the user is seeking. In that case, the player will advance to the closest keyframe before or after the specified time as dictated by the
seek()method of the Flash player'sNetStreamobject. (See Adobe's documentation for more information.) -
The
allowSeekAheadparameter determines whether the player will make a new request to the server if thesecondsparameter specifies a time outside of the currently buffered video data.We recommend that you set this parameter to
falsewhile the user drags the mouse along a video progress bar and then set it totruewhen the user releases the mouse. This approach lets a user scroll to different points of a video without requesting new video streams by scrolling past unbuffered points in the video. When the user releases the mouse button, the player advances to the desired point in the video and requests a new video stream if necessary.
-
player.clearVideo():Void- Clears the video display. This function is useful if you want to clear the video remnant after calling
stopVideo(). Note that this function has been deprecated in the ActionScript 3.0 Player API. player.nextVideo():Void- This function loads and plays the next video in the playlist.
-
If
player.nextVideo()is called while the last video in the playlist is being watched, and the playlist is set to play continuously (loop), then the player will load and play the first video in the list. -
If
player.nextVideo()is called while the last video in the playlist is being watched, and the playlist is not set to play continuously, then playback will end.
-
player.previousVideo():Void- This function loads and plays the previous video in the playlist.
-
If
player.previousVideo()is called while the first video in the playlist is being watched, and the playlist is set to play continuously (loop), then the player will load and play the last video in the list. -
If
player.previousVideo()is called while the first video in the playlist is being watched, and the playlist is not set to play continuously, then the player will restart the first playlist video from the beginning.
-
player.playVideoAt(index:Number):Void- This function loads and plays the specified video in the playlist.
-
The required
indexparameter specifies the index of the video that you want to play in the playlist. The parameter uses a zero-based index, so a value of0identifies the first video in the list. If you have shuffled the playlist, this function will play the video at the specified position in the shuffled playlist.
-
player.mute():Void- Mutes the player.
player.unMute():Void- Unmutes the player.
player.isMuted():Boolean- Returns
trueif the player is muted,falseif not. player.setVolume(volume:Number):Void- Sets the volume. Accepts an integer between
0and100. player.getVolume():Number- Returns the player's current volume, an integer between
0and100. Note thatgetVolume()will return the volume even if the player is muted. player.getPlaybackRate():Number- This function retrieves the playback rate of the currently playing video. The default playback rate is
1, which indicates that the video is playing at normal speed. Playback rates may include values like0.25,0.5,1,1.5, and2. player.setPlaybackRate(suggestedRate:Number):Void- This function sets the suggested playback rate for the current video. If the playback rate changes, it will only change for the video that is already cued or being played. If you set the playback rate for a cued video, that rate will still be in effect when the
playVideofunction is called or the user initiates playback directly through the player controls. In addition, calling functions to cue or load videos or playlists (cueVideoById,loadVideoById, etc.) will reset the playback rate to1.
Calling this function does not guarantee that the playback rate will actually change. However, if the playback rate does change, theonPlaybackRateChangeevent will fire, and your code should respond to the event rather than the fact that it called thesetPlaybackRatefunction.
ThegetAvailablePlaybackRatesmethod will return the possible playback rates for the currently playing video. However, if you set thesuggestedRateparameter to a non-supported integer or float value, the player will round that value down to the nearest supported value in the direction of1.
Note: Even though the AS3 player supports playback rate controls, variable speeds are currently only supported in the HTML5 player.
player.getAvailablePlaybackRates():Array- This function returns the set of playback rates in which the current video is available. The default value is
1, which indicates that the video is playing in normal speed.
The function returns an array of numbers ordered from slowest to fastest playback speed. Even if the player does not support variable playback speeds, the array should always contain at least one value (1). player.setLoop(loopPlaylists:Boolean):Void-
This function indicates whether the video player should continuously play a playlist or if it should stop playing after the last video in the playlist ends. The default behavior is that playlists do not loop.
This setting will persist even if you load or cue a different playlist, which means that if you load a playlist, call the
setLoopfunction with a value oftrue, and then load a second playlist, the second playlist will also loop.-
The required
loopPlaylistsparameter identifies the looping behavior.-
If the parameter value is
true, then the video player will continuously play playlists. After playing the last video in a playlist, the video player will go back to the beginning of the playlist and play it again. -
If the parameter value is
false, then playbacks will end after the video player plays the last video in a playlist.
-
-
player.setShuffle(shufflePlaylist:Boolean):Void-
This function indicates whether a playlist's videos should be shuffled so that they play back in an order different from the one that the playlist creator designated. If you shuffle a playlist after it has already started playing, the list will be reordered while the video that is playing continues to play. The next video that plays will then be selected based on the reordered list.
This setting will not persist if you load or cue a different playlist, which means that if you load a playlist, call the
setShufflefunction, and then load a second playlist, the second playlist will not be shuffled.-
The required
shufflePlaylistparameter indicates whether YouTube should shuffle the playlist.-
If the parameter value is
true, then YouTube will shuffle the playlist order. If you instruct the function to shuffle a playlist that has already been shuffled, YouTube will shuffle the order again. -
If the parameter value is
false, then YouTube will change the playlist order back to its original order.
-
-
Queueing functions
Queueing functions allow you to load and play a video, a playlist, or another list of videos. If you are using the object syntax described below to call these functions, then you can also queue or load a list of search results or a user's list of uploaded videos.
The API supports two different syntaxes for calling the queueing functions.
For example, the loadVideoById function can be called in either of the following ways. Note that the object syntax supports the endSeconds property, which the argument syntax does not support.
The different queueing functions for videos and playlists are described below.
Queueing functions for videos
Queueing functions for lists
The cuePlaylist and loadPlaylist functions allow you to load and play a playlist or list of videos.
If you are using the object syntax to call these functions, you can also queue (or load) a list of search results or a user's list of uploaded videos.
Since the functions work differently depending on whether they are called using the argument syntax or the object syntax, both calling methods are documented below.
