There are two parameters to consider here: |
1. The first parameter specifies either the DOM element of the id or the HTML element |
2. The second parameter specifies the video player options |
In simpler terms, the first parameter replaces it with the iframe
element which contains the player. Keep in mind that this can also change the layout of the page if the parameter replaced has a different display style than the iframe
.
The second parameter is where all of the personalizing comes in. The object contains 3 main properties:
Property |
Function |
width |
This is a number and specifies the width of the video player. (Default: 640) |
</tr>
height |
This is a number and specifies the height of the video player. (Default: 390) |
videoId |
This is the ID of the video that will play. It can be found at the end of the url. (Ex: for https://www.youtube.com/watch?v=0HTexqxo1og then "0HTexqxo1og" is the ID of the video. |
The onPlayerReady
event ensures that the video will start when the page opens which is shown here by the video in the beginning.
Functions
Queueing Functions
Now that we can get our video up and running what if we want to not play it immediately? For example wait 5 seconds to start the video. This is where functions come in. We will only touch briefly on this because it the information on this goes into great detail and we are looking more at the main idea.
There are 2 types of syntax that make this happen.
Argument syntax
loadVideoById("bHQqvYy5KYo", 5, "large")
Object syntax
loadVideoById({'videoId': 'bHQqvYy5KYo',
'startSeconds': 5,
'endSeconds': 60,
'suggestedQuality': 'large'});
The API supports the two different types of syntax. The first type, argument syntax, requires that the function arguments be listed in order. The second type, object syntax, lets you pass an object as a single parameter to define the properties and may support additional things that the argument syntax does not.
If you want to start the video 5 seconds in and not end it at a certain time then argument syntax is the way to go. If you want to start the video 5 seconds in and end it after 60 seconds then object syntax gives the option to do that.
Functions
Without going into too much detail, a brief overview of other functions available include the following. Keep in mind that each of these also have options for argument syntax as well as object syntax.
Function |
Description |
cueVideoById |
This function loads the video’s thumbnails and prepares the player to play the video |
</tr>
loadVideoById |
This function loads and plays the specified video |
cueVideoByUrl |
This function does the same this as cueVideoById but uses an URL |
loadVideoByUrl |
This function does the same thing as loadVideoById but uses an URL |
You can even make functions for playlists! Both of the following functions allow you to play a playlist of videos. Each work a little differently and therefore have different properties.
Function |
Code |
Description |
cuePlaylist |
player.cuePlaylist(playlist:String|Array,
index:Number,
startSeconds:Number,
suggestedQuality:String):Void |
This function queues the specified playlist. It takes an array of YouTube video IDs and an optional parameter to play the first video in the playlist. Again you can customize it with the startSeconds and suggestedQuality |
loadPlaylist |
player.loadPlaylist(playlist:String|Array,
index:Number,
startSeconds:Number,
suggestedQuality:String):Void |
This function loads the specified playlist and plays it. Again it takes an array of video ID’s with the optional parameter with startSeconds and suggestedQuality . |
Playback Controls
Playing a Video
Up until now we have talked about getting the player set up with basic functions. Now we want to look at ways to customize how it plays the video. There are a lot of different options here. We also can look at what the final player state is after the function executes.
Function |
Description |
Final Player State |
player.playVideo():Void |
Plays the currently cued video |
playing (1) |
player.pauseVideo():Void |
Pauses the currently playing video |
paused (2) |
player.stopVideo():Void |
Stops video and stops loading of the video. Warning: This function is not used often and should only be used when you know the user will not be watching any additional videos in the player |
Can put the player into any non-playing state |
Playing a video - Playlist
Function |
Description |
player.nextVideo():Void |
This function loads and plays the next video in the playlist. It can even be set to continuously loop or just end |
player.previousVideo():Void |
This function load and plays the previous video in the playlist. Again can be set to a loop |
player.playVideoAt(index:Number):Void |
This function loads and plays the specified video in the playlist. Keep in mind you have to include an index parameter because you have to specify the index of the video that you want to play |
There are many more ways to personalize the video player but here are a few generally listed:
Changing the Player Volume |
1. player.mute():Void |
2. player.unMute():Void |
3. player.isMuted():Boolean |
4. player.setVolume(volume:Number):Void |
5. player.getVolume():Number |
Setting the player size |
1. player.setSize(width:Number, height:Number):Object |
Setting the playback rate |
1. player.getPlaybackRate():Number |
Playing a video - Playback Status
Often the functions return a status which usually comes in the form of a number. Here are the possible values and what they mean.
-1 – unstarted
0 – ended
1 – playing
2 – paused
3 – buffering
5 – video cued
Event Handlers
Last thing to address is Events. At this point you should have a good understanding on how to get everything set up and personalized. There are many different types of events, all however are equally important.
Event Handlers |
Description |
onReady |
onReady This event fires when a player has finished loading. A good example is playing the video automatically. As you can tell from the video when this website loaded, it has been coded with the onReady event. |
onPlaybackQualityChange |
This event fires when the video playback quality changes. The data property value possible values are:
- small
- medium
- large
- hd720
- hd1080
- highres
|
onPlaybackRateChange |
This event fires whenever the video playback rate changes |
onError |
This event fires if an error occurs in the player. Here are a few possible errors that could occur:
- 2 - if the request contains an invalid parameter value
- 5 - any error associated with HTML5, such as the HTML5 player not supporting the video
- 100 - the video requested was not found
- 101 - the owner of the video does not allow it to be played in an embedded player
- 150 - the error is the same as 101
|
Example Video
The example video is shown at the top of the page!