Hướng dẫn tạo file api https www.youtube.com watch v 6gudzjgdfsw năm 2024

The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript.

Using the API's JavaScript functions, you can queue videos for playback; play, pause, or stop those videos; adjust the player volume; or retrieve information about the video being played. You can also add event listeners that will execute in response to certain player events, such as a player state change.

This guide explains how to use the IFrame API. It identifies the different types of events that the API can send and explains how to write event listeners to respond to those events. It also details the different JavaScript functions that you can call to control the video player as well as the player parameters you can use to further customize the player.

Requirements

The user's browser must support the HTML5

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

5 feature. Most modern browsers support

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

5.

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.

Any web page that uses the IFrame API must also implement the following JavaScript function:

  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 – The API will call this function when the page has finished downloading the JavaScript for the player API, which enables you to then use the API on your page. Thus, this function might create the player objects that you want to display when the page loads.

Getting started

The sample HTML page below creates an embedded player that will load a video, play it for six seconds, and then stop the playback. The numbered comments in the HTML are explained in the list below the example.

<!DOCTYPE html> <html> <body>

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      playerVars: {
        'playsinline': 1
      },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }
  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }
  // 5. The API calls this function when the player's state changes.
  //    The function indicates that when playing a video (state=1),
  //    the player should play for six seconds and then stop.
  var done = false;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING && !done) {
      setTimeout(stopVideo, 6000);
      done = true;
    }
  }
  function stopVideo() {
    player.stopVideo();
  }
</script>
</body> </html>

The following list provides more details about the sample above:

  1. The <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 8 tag in this section identifies the location on the page where the IFrame API will place the video player. The constructor for the player object, which is described in the section, identifies the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 8 tag by its loadVideoById("bHQqvYy5KYo", 5, "large") 0 to ensure that the API places the loadVideoById("bHQqvYy5KYo", 5, "large") 1 in the proper location. Specifically, the IFrame API will replace the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 8 tag with the loadVideoById("bHQqvYy5KYo", 5, "large") 1 tag. As an alternative, you could also put the loadVideoById("bHQqvYy5KYo", 5, "large") 1 element directly on the page. The section explains how to do so.
  2. The code in this section loads the IFrame Player API JavaScript code. The example uses DOM modification to download the API code to ensure that the code is retrieved asynchronously. (The loadVideoById("bHQqvYy5KYo", 5, "large") 5 tag's loadVideoById("bHQqvYy5KYo", 5, "large") 6 attribute, which also enables asynchronous downloads, is not yet supported in all modern browsers as discussed in this Stack Overflow answer.
  3. The <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 function will execute as soon as the player API code downloads. This portion of the code defines a global variable, loadVideoById("bHQqvYy5KYo", 5, "large") 8, which refers to the video player you are embedding, and the function then constructs the video player object.
  4. The loadVideoById("bHQqvYy5KYo", 5, "large") 9 function will execute when the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    0 event fires. In this example, the function indicates that when the video player is ready, it should begin to play.
  5. The API will call the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    1 function when the player's state changes, which may indicate that the player is playing, paused, finished, and so forth. The function indicates that when the player state is loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    2 (playing), the player should play for six seconds and then call the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    3 function to stop the video.

Loading a video player

After the API's JavaScript code loads, the API will call the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

7 function, at which point you can construct a

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
5 object to insert a video player on your page. The HTML excerpt below shows the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

7 function from the example above:

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

The constructor for the video player specifies the following parameters:

  1. The first parameter specifies either the DOM element or the loadVideoById("bHQqvYy5KYo", 5, "large") 0 of the HTML element where the API will insert the loadVideoById("bHQqvYy5KYo", 5, "large") 1 tag containing the player. The IFrame API will replace the specified element with the loadVideoById("bHQqvYy5KYo", 5, "large") 1 element containing the player. This could affect the layout of your page if the element being replaced has a different display style than the inserted loadVideoById("bHQqvYy5KYo", 5, "large") 1 element. By default, an loadVideoById("bHQqvYy5KYo", 5, "large") 1 displays as an player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    2 element.
  2. The second parameter is an object that specifies player options. The object contains the following properties:
    • player.cueVideoById(videoId:String,
                         startSeconds:Number):Void  
      
      3 (number) – The width of the video player. The default value is player.cueVideoById(videoId:String,
                         startSeconds:Number):Void  
    • player.cueVideoById(videoId:String, startSeconds:Number):Void 5 (number) – The height of the video player. The default value is player.cueVideoById(videoId:String,
                         startSeconds:Number):Void  
    • player.cueVideoById(videoId:String, startSeconds:Number):Void 7 (string) – The YouTube video ID that identifies the video that the player will load.
    • player.cueVideoById(videoId:String,
                         startSeconds:Number):Void  
      
      8 (object) – The object's properties identify player parameters that can be used to customize the player.
    • player.cueVideoById(videoId:String,
                         startSeconds:Number):Void  
      
      9 (object) – The object's properties identify the events that the API fires and the functions (event listeners) that the API will call when those events occur. In the example, the constructor indicates that the loadVideoById("bHQqvYy5KYo", 5, "large") 9 function will execute when the loadVideoById({'videoId': 'bHQqvYy5KYo',
                    'startSeconds': 5,  
                    'endSeconds': 60});  
      
      0 event fires and that the loadVideoById({'videoId': 'bHQqvYy5KYo',
                    'startSeconds': 5,  
                    'endSeconds': 60});  
      
      1 function will execute when the player.cueVideoById({videoId:String,
                          startSeconds:Number,  
                          endSeconds:Number}):Void  
      
      3 event fires.

As mentioned in the section, instead of writing an empty

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

8 element on your page, which the player API's JavaScript code will then replace with an

loadVideoById("bHQqvYy5KYo", 5, "large")

1 element, you could create the

loadVideoById("bHQqvYy5KYo", 5, "large")

1 tag yourself. The first example in the section shows how to do this.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

Note that if you do write the

loadVideoById("bHQqvYy5KYo", 5, "large")

1 tag, then when you construct the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
5 object, you do not need to specify values for the

player.cueVideoById(videoId:String,

                startSeconds:Number):Void
3 and

player.cueVideoById(videoId:String,

                startSeconds:Number):Void
5, which are specified as attributes of the

loadVideoById("bHQqvYy5KYo", 5, "large")

1 tag, or the

player.cueVideoById(videoId:String,

                startSeconds:Number):Void
7 and player parameters, which are are specified in the

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void
3 URL. As an extra security measure, you should also include the

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void
4 parameter to the URL, specifying the URL scheme (

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void
5 or

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void

  1. and full domain of your host page as the parameter value. While

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void
4 is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.

The section also shows a couple other examples for constructing video player objects.

Operations

To call the player API methods, you must first get a reference to the player object you wish to control. You obtain the reference by creating a

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
5 object as discussed in the and sections of this document.

Functions

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 a user's uploaded videos.

The API supports two different syntaxes for calling the queueing functions.

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

For example, the

player.loadVideoById(videoId:String,

                 startSeconds:Number):Void
9 function can be called in either of the following ways. Note that the object syntax supports the

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
0 property, which the argument syntax does not support.

  • Argument syntax loadVideoById("bHQqvYy5KYo", 5, "large")
  • Object syntax

    loadVideoById({'videoId': 'bHQqvYy5KYo',

               'startSeconds': 5,  
               'endSeconds': 60});

Queueing functions for videos

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
1

  • Argument syntax

    player.cueVideoById(videoId:String,

                    startSeconds:Number):Void
  • Object syntax player.cueVideoById({videoId:String,
                     startSeconds:Number,  
                     endSeconds:Number}):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

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
2 or

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
3 is called.

  • The required player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    7 parameter specifies the YouTube Video ID of the video to be played. In the YouTube Data API, a player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    5 resource's player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    6 property specifies the ID.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 parameter accepts a float/integer and specifies the time from which the video should start playing when player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    2 is called. If you specify a player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 value and then call player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3, then the player plays from the time specified in the player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3 call. When the video is cued and ready to play, the player will broadcast a ( player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3).
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 parameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing when player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    2 is called. If you specify an player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 value and then call player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3, the player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 value will no longer be in effect.

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void
9

  • Argument syntax

    player.loadVideoById(videoId:String,

                     startSeconds:Number):Void
  • Object syntax player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void
This function loads and plays the specified video.

  • The required player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    7 parameter specifies the YouTube Video ID of the video to be played. In the YouTube Data API, a player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    5 resource's player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    6 property specifies the ID.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 parameter accepts a float/integer. If it is specified, then the video will start from the closest keyframe to the specified time.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 parameter accepts a float/integer. If it is specified, then the video will stop playing at the specified time.

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

05

  • Argument syntax player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void
  • Object syntax var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 0

This function loads the specified video's thumbnail and prepares the player to play the video. The player does not request the FLV until

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
2 or

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
3 is called.

  • The required var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 08 parameter specifies a fully qualified YouTube player URL in the format var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 09.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 parameter accepts a float/integer and specifies the time from which the video should start playing when player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    2 is called. If you specify player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 and then call player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3, then the player plays from the time specified in the player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3 call. When the video is cued and ready to play, the player will broadcast a (5).
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 parameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing when player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    2 is called. If you specify an player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 value and then call player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    3, the player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 value will no longer be in effect.

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

21

  • Argument syntax var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 1
  • Object syntax var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 2

This function loads and plays the specified video.

  • The required var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 08 parameter specifies a fully qualified YouTube player URL in the format var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 09.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 parameter accepts a float/integer and specifies the time from which the video should start playing. If player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    7 (number can be a float) is specified, the video will start from the closest keyframe to the specified time.
  • The optional player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 parameter, which is only supported in object syntax, accepts a float/integer and specifies the time when the video should stop playing.

Queueing functions for lists

The

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

27 and

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

28 functions allow you to load and play a playlist. If you are using object syntax to call these functions, you can also queue (or load) a list of a user's 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.

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

27

  • Argument syntax

    var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 3 Queues the specified playlist. When the playlist is cued and ready to play, the player will broadcast a ( player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3).

    • The required var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 32 parameter specifies an array of YouTube video IDs. In the YouTube Data API, the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      5 resource's player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      6 property identifies that video's ID.
    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 35 parameter 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 is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 36, so the default behavior is to load and play the first video in the playlist.
    • The optional player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 parameter accepts a float/integer and specifies the time from which the first video in the playlist should start playing when the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      2 function is called. If you specify a player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 value and then call player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      3, then the player plays from the time specified in the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      3 call. If you cue a playlist and then call the var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 42 function, the player will start playing at the beginning of the specified video.
  • Object syntax

    var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 4 Queues the specified list of videos. The list can be a playlist or a user's uploaded videos feed. The ability to queue a list of search results is and will no longer be supported as of 15 November 2020. When the list is cued and ready to play, the player will broadcast a ( player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3).

    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 45 property specifies the type of results feed that you are retrieving. Valid values are var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 32 and var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 47. A deprecated value, var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 48, will no longer be supported as of 15 November 2020. The default value is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      }
    • The required

      var player; function onYouTubeIframeAPIReady() {

       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 50 property contains a key that identifies the particular list of videos that YouTube should return.

      • If the
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             45 property value is  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             32, then the  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             50 property specifies the playlist ID or an array of video IDs. In the YouTube Data API, the  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             32 resource's  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             55 property identifies a playlist's ID, and the  
             player.loadVideoById({videoId:String,  
                                   startSeconds:Number,  
                                   endSeconds:Number}):Void  
             5 resource's  
             player.loadVideoById({videoId:String,  
                                   startSeconds:Number,  
                                   endSeconds:Number}):Void  
             6 property specifies a video ID.  
      • If the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 45 property value is var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 47, then the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 50 property identifies the user whose uploaded videos will be returned.
      • If the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 45 property value is var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 48, then the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 50 property specifies the search query. Note: This functionality is and will no longer be supported as of 15 November 2020.
    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 35 property 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 is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 36, so the default behavior is to load and play the first video in the list.
    • The optional player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 property accepts a float/integer and specifies the time from which the first video in the list should start playing when the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      2 function is called. If you specify a player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 value and then call player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      3, then the player plays from the time specified in the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      3 call. If you cue a list and then call the var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 42 function, the player will start playing at the beginning of the specified video.

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

28

  • Argument syntax

    var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 5 This function loads the specified playlist and plays it.

    • The required var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 32 parameter specifies an array of YouTube video IDs. In the YouTube Data API, the player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      5 resource's player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      6 property specifies a video ID.
    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 35 parameter 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 is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 36, so the default behavior is to load and play the first video in the playlist.
    • The optional player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 parameter accepts a float/integer and specifies the time from which the first video in the playlist should start playing.
  • Object syntax

    var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 6 This function loads the specified list and plays it. The list can be a playlist or a user's uploaded videos feed. The ability to load a list of search results is and will no longer be supported as of 15 November 2020.

    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 45 property specifies the type of results feed that you are retrieving. Valid values are var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 32 and var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 47. A deprecated value, var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 48, will no longer be supported as of 15 November 2020. The default value is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      }
    • The required

      var player; function onYouTubeIframeAPIReady() {

       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 50 property contains a key that identifies the particular list of videos that YouTube should return.

      • If the
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             45 property value is  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             32, then the  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             50 property specifies a playlist ID or an array of video IDs. In the YouTube Data API, the  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             32 resource's  
             var player;  
             function onYouTubeIframeAPIReady() {  
               player = new YT.Player('player', {  
                 height: '390',  
                 width: '640',  
                 videoId: 'M7lc1UVf-VE',  
                 playerVars: {  
                   'playsinline': 1  
                 },  
                 events: {  
                   'onReady': onPlayerReady,  
                   'onStateChange': onPlayerStateChange  
                 }  
               });  
             }  
             55 property specifies a playlist's ID, and the  
             player.loadVideoById({videoId:String,  
                                   startSeconds:Number,  
                                   endSeconds:Number}):Void  
             5 resource's  
             player.loadVideoById({videoId:String,  
                                   startSeconds:Number,  
                                   endSeconds:Number}):Void  
             6 property specifies a video ID.  
      • If the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 45 property value is var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 47, then the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 50 property identifies the user whose uploaded videos will be returned.
      • If the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 45 property value is var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 48, then the var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', playerVars: { 'playsinline': 1 }, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } 50 property specifies the search query. Note: This functionality is and will no longer be supported as of 15 November 2020.
    • The optional var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 35 property 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 is var player; function onYouTubeIframeAPIReady() {
       player = new YT.Player('player', {  
         height: '390',  
         width: '640',  
         videoId: 'M7lc1UVf-VE',  
         playerVars: {  
           'playsinline': 1  
         },  
         events: {  
           'onReady': onPlayerReady,  
           'onStateChange': onPlayerStateChange  
         }  
       });  
      
      } 36, so the default behavior is to load and play the first video in the list.
    • The optional player.loadVideoById({videoId:String,
                           startSeconds:Number,  
                           endSeconds:Number}):Void  
      
      7 property accepts a float/integer and specifies the time from which the first video in the list should start playing.

Playback controls and player settings

Playing a video

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

01 Plays the currently cued/loaded video. The final player state after this function executes will be

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

02 (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.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

03 Pauses the currently playing video. The final player state after this function executes will be

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

04 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

  1. unless the player is in the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

06 (

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

  1. state when the function is called, in which case the player state will not change.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

08 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

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

09 function. If you want to change the video that the player is playing, you can call one of the queueing functions without calling

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
3 first.

Important: Unlike the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

09 function, which leaves the player in the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

04 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

  1. state, the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
3 function could put the player into any not-playing state, including

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

06 (

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36),

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

04 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

05),

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void
2 (

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void

  1. or

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

21 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

22).

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

23 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 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

02,

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void
2, etc.), the player will play the video.

  • The <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 26 parameter 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.
  • The <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 27 parameter determines whether the player will make a new request to the server if the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 26 parameter specifies a time outside of the currently buffered video data. We recommend that you set this parameter to <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 29 while the user drags the mouse along a video progress bar and then set it to <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 30 when 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.

Controlling playback of 360° videos

Note: The 360° video playback experience has limited support on mobile devices. On unsupported devices, 360° videos appear distorted and there is no supported way to change the viewing perspective at all, including through the API, using orientation sensors, or responding to touch/drag actions on the device's screen.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

31 Retrieves properties that describe the viewer's current perspective, or view, for a video playback. In addition:

  • This object is only populated for 360° videos, which are also called spherical videos.
  • If the current video is not a 360° video or if the function is called from a non-supported device, then the function returns an empty object.
  • On supported mobile devices, if the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 32 property is set to <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 30, then this function returns an object in which the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 34 property contains the correct value and the other properties are set to var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36. The object contains the following properties: Properties

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

36 A number in the range [0, 360) that represents the horizontal angle of the view in degrees, which reflects the extent to which the user turns the view to face further left or right. The neutral position, facing the center of the video in its equirectangular projection, represents 0°, and this value increases as the viewer turns left.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

37 A number in the range [-90, 90] that represents the vertical angle of the view in degrees, which reflects the extent to which the user adjusts the view to look up or down. The neutral position, facing the center of the video in its equirectangular projection, represents 0°, and this value increases as the viewer looks up.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

38 A number in the range [-180, 180] that represents the clockwise or counterclockwise rotational angle of the view in degrees. The neutral position, with the horizontal axis in the equirectangular projection being parallel to the horizontal axis of the view, represents 0°. The value increases as the view rotates clockwise and decreases as the view rotates counterclockwise.

Note that the embedded player does not present a user interface for adjusting the roll of the view. The roll can be adjusted in either of these mutually exclusive ways:

  1. Use the orientation sensor in a mobile browser to provide roll for the view. If the is enabled, then the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 39 function always returns var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36 as the value of the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 38 property.
  2. If the orientation sensor is disabled, set the roll to a nonzero value using this API.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

34 A number in the range [30, 120] that represents the field-of-view of the view in degrees as measured along the longer edge of the viewport. The shorter edge is automatically adjusted to be proportional to the aspect ratio of the view.

The default value is 100 degrees. Decreasing the value is like zooming in on the video content, and increasing the value is like zooming out. This value can be adjusted either by using the API or by using the mousewheel when the video is in fullscreen mode.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

43 Sets the video orientation for playback of a 360° video. (If the current video is not spherical, the method is a no-op regardless of the input.)

The player view responds to calls to this method by updating to reflect the values of any known properties in the

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

44 object. The view persists values for any other known properties not included in that object.

In addition:

  • If the object contains unknown and/or unexpected properties, the player ignores them.
  • As noted at the beginning of this section, the 360° video playback experience is not supported on all mobile devices.
  • By default, on supported mobile devices, this function sets only sets the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 34 property and does not affect the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 36, <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 37, and <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 38 properties for 360° video playbacks. See the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 49 property below for more detail. The

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

44 object passed to the function contains the following properties: Properties

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

36 See above.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

37 See above.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

38 See above.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

34 See above.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

49Note: This property affects the 360° viewing experience on supported devices only.A boolean value that indicates whether the IFrame embed should respond to events that signal changes in a supported device's orientation, such as a mobile browser's

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

56. The default parameter value is

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

30.

Supported mobile devices

  • When the value is <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 30, an embedded player relies only on the device's movement to adjust the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 36, <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 37, and <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 38 properties for 360° video playbacks. However, the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 34 property can still be changed via the API, and the API is, in fact, the only way to change the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 34 property on a mobile device. This is the default behavior.
  • When the value is

    <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 29, then the device's movement does not affect the 360° viewing experience, and the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 36, <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 37, <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 38, and <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 34 properties must all be set via the API.

    Unsupported mobile devices The

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

49 property value does not have any effect on the playback experience.

Playing a video in a playlist

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

70 This function loads and plays the next video in the playlist.

  • If <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 71 is called while the last video in the playlist is being watched, and the playlist is set to play continuously ( <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 72), then the player will load and play the first video in the list.
  • If <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 71 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.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

74 This function loads and plays the previous video in the playlist.

  • If <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 75 is called while the first video in the playlist is being watched, and the playlist is set to play continuously ( <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 72), then the player will load and play the last video in the list.
  • If <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 75 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.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

78 This function loads and plays the specified video in the playlist.

  • The required var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 35 parameter specifies the index of the video that you want to play in the playlist. The parameter uses a zero-based index, so a value of var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36 identifies the first video in the list. If you have the playlist, this function will play the video at the specified position in the shuffled playlist.

Changing the player volume

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

81 Mutes the player.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

82 Unmutes the player.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

83 Returns

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

30 if the player is muted,

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

29 if not.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

86 Sets the volume. Accepts an integer between

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36 and

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

88.

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

89 Returns the player's current volume, an integer between

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36 and

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

88. Note that

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

92 will return the volume even if the player is muted.

Setting the player size

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

93 Sets the size in pixels of the

loadVideoById("bHQqvYy5KYo", 5, "large")

1 that contains the player.

Setting the playback rate

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

95 This function retrieves the playback rate of the currently playing video. The default playback rate is

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2, which indicates that the video is playing at normal speed. Playback rates may include values like

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

97,

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

98,

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2,

loadVideoById("bHQqvYy5KYo", 5, "large")

00, and

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

05.

loadVideoById("bHQqvYy5KYo", 5, "large")

02 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

loadVideoById("bHQqvYy5KYo", 5, "large")

03 function is called or the user initiates playback directly through the player controls. In addition, calling functions to cue or load videos or playlists (

player.loadVideoById({videoId:String,

                  startSeconds:Number,
                  endSeconds:Number}):Void
1,

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void
9, etc.) will reset the playback rate to

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2.

Calling this function does not guarantee that the playback rate will actually change. However, if the playback rate does change, the

loadVideoById("bHQqvYy5KYo", 5, "large")

07 event will fire, and your code should respond to the event rather than the fact that it called the

loadVideoById("bHQqvYy5KYo", 5, "large")

08 function.

The

loadVideoById("bHQqvYy5KYo", 5, "large")

09 method will return the possible playback rates for the currently playing video. However, if you set the

loadVideoById("bHQqvYy5KYo", 5, "large")

10 parameter to a non-supported integer or float value, the player will round that value down to the nearest supported value in the direction of

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2.

loadVideoById("bHQqvYy5KYo", 5, "large")

12 This function returns the set of playback rates in which the current video is available. The default value is

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2, 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 (

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2).

Setting playback behavior for playlists

loadVideoById("bHQqvYy5KYo", 5, "large")

15

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

loadVideoById("bHQqvYy5KYo", 5, "large")

16 function with a value of

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

30, and then load a second playlist, the second playlist will also loop.

The required

loadVideoById("bHQqvYy5KYo", 5, "large")

18 parameter identifies the looping behavior.

  • If the parameter value is <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 30, 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 <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 29, then playbacks will end after the video player plays the last video in a playlist.

loadVideoById("bHQqvYy5KYo", 5, "large")

21

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

loadVideoById("bHQqvYy5KYo", 5, "large")

22 function, and then load a second playlist, the second playlist will not be shuffled.

The required

loadVideoById("bHQqvYy5KYo", 5, "large")

23 parameter indicates whether YouTube should shuffle the playlist.

  • If the parameter value is <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 30, 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 <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 29, then YouTube will change the playlist order back to its original order.

Playback status

loadVideoById("bHQqvYy5KYo", 5, "large")

26 Returns a number between

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36 and

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2 that specifies the percentage of the video that the player shows as buffered. This method returns a more reliable number than the now-deprecated

loadVideoById("bHQqvYy5KYo", 5, "large")

29 and

loadVideoById("bHQqvYy5KYo", 5, "large")

30 methods.

loadVideoById("bHQqvYy5KYo", 5, "large")

31 Returns the state of the player. Possible values are:

  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 22 – unstarted
  • var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36 – ended
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    2 – playing
  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 05 – paused
  • loadVideoById("bHQqvYy5KYo", 5, "large") 36 – buffering
  • player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3 – video cued

loadVideoById("bHQqvYy5KYo", 5, "large")

38 Returns the elapsed time in seconds since the video started playing.

loadVideoById("bHQqvYy5KYo", 5, "large")

39Deprecated as of October 31, 2012. Returns the number of bytes the video file started loading from. (This method now always returns a value of

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36.) Example scenario: the user seeks ahead to a point that hasn't loaded yet, and the player makes a new request to play a segment of the video that hasn't loaded yet.

loadVideoById("bHQqvYy5KYo", 5, "large")

41Deprecated as of July 18, 2012. Instead, use the

loadVideoById("bHQqvYy5KYo", 5, "large")

42 method to determine the percentage of the video that has buffered.

This method returns a value between

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36 and

loadVideoById("bHQqvYy5KYo", 5, "large")

44 that approximates the amount of the video that has been loaded. You could calculate the fraction of the video that has been loaded by dividing the

loadVideoById("bHQqvYy5KYo", 5, "large")

45 value by the

loadVideoById("bHQqvYy5KYo", 5, "large")

46 value.

loadVideoById("bHQqvYy5KYo", 5, "large")

47Deprecated as of July 18, 2012. Instead, use the

loadVideoById("bHQqvYy5KYo", 5, "large")

42 method to determine the percentage of the video that has buffered.

Returns the size in bytes of the currently loaded/playing video or an approximation of the video's size.

This method always returns a value of

loadVideoById("bHQqvYy5KYo", 5, "large")

44. You could calculate the fraction of the video that has been loaded by dividing the

loadVideoById("bHQqvYy5KYo", 5, "large")

45 value by the

loadVideoById("bHQqvYy5KYo", 5, "large")

46 value.

Retrieving video information

loadVideoById("bHQqvYy5KYo", 5, "large")

52 Returns the duration in seconds of the currently playing video. Note that

loadVideoById("bHQqvYy5KYo", 5, "large")

53 will return

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36 until the video's metadata is loaded, which normally happens just after the video starts playing.

If the currently playing video is a live event, the

loadVideoById("bHQqvYy5KYo", 5, "large")

53 function will return the elapsed time since the live video stream began. Specifically, this is the amount of time that the video has streamed without being reset or interrupted. In addition, this duration is commonly longer than the actual event time since streaming may begin before the event's start time.

loadVideoById("bHQqvYy5KYo", 5, "large")

56 Returns the YouTube.com URL for the currently loaded/playing video.

loadVideoById("bHQqvYy5KYo", 5, "large")

57 Returns the embed code for the currently loaded/playing video.

Retrieving playlist information

loadVideoById("bHQqvYy5KYo", 5, "large")

58 This function returns an array of the video IDs in the playlist as they are currently ordered. By default, this function will return video IDs in the order designated by the playlist owner. However, if you have called the

loadVideoById("bHQqvYy5KYo", 5, "large")

59 function to shuffle the playlist order, then the

loadVideoById("bHQqvYy5KYo", 5, "large")

60 function's return value will reflect the shuffled order.

loadVideoById("bHQqvYy5KYo", 5, "large")

61 This function returns the index of the playlist video that is currently playing.

  • If you have not shuffled the playlist, the return value will identify the position where the playlist creator placed the video. The return value uses a zero-based index, so a value of var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36 identifies the first video in the playlist.
  • If you have shuffled the playlist, the return value will identify the video's order within the shuffled playlist.

Adding or removing an event listener

loadVideoById("bHQqvYy5KYo", 5, "large")

63 Adds a listener function for the specified

loadVideoById("bHQqvYy5KYo", 5, "large")

64. The section below identifies the different events that the player might fire. The listener is a string that specifies the function that will execute when the specified event fires.

loadVideoById("bHQqvYy5KYo", 5, "large")

65 Removes a listener function for the specified

loadVideoById("bHQqvYy5KYo", 5, "large")

64. The

loadVideoById("bHQqvYy5KYo", 5, "large")

67 is a string that identifies the function that will no longer execute when the specified event fires.

Accessing and modifying DOM nodes

loadVideoById("bHQqvYy5KYo", 5, "large")

68 This method returns the DOM node for the embedded

loadVideoById("bHQqvYy5KYo", 5, "large")

1.

loadVideoById("bHQqvYy5KYo", 5, "large")

70 Removes the

loadVideoById("bHQqvYy5KYo", 5, "large")

1 containing the player.

Events

The API fires events to notify your application of changes to the embedded player. As noted in the previous section, you can subscribe to events by adding an event listener when , and you can also use the

loadVideoById("bHQqvYy5KYo", 5, "large")

73 function.

The API will pass an event object as the sole argument to each of those functions. The event object has the following properties:

  • The event's loadVideoById("bHQqvYy5KYo", 5, "large") 74 identifies the video player that corresponds to the event.
  • The event's loadVideoById("bHQqvYy5KYo", 5, "large") 75 specifies a value relevant to the event. Note that the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    0 and loadVideoById("bHQqvYy5KYo", 5, "large") 77 events do not specify a loadVideoById("bHQqvYy5KYo", 5, "large") 75 property.

The following list defines the events that the API fires:

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
0 This event fires whenever a player has finished loading and is ready to begin receiving API calls. Your application should implement this function if you want to automatically execute certain operations, such as playing the video or displaying information about the video, as soon as the player is ready.

The example below shows a sample function for handling this event. The event object that the API passes to the function has a

loadVideoById("bHQqvYy5KYo", 5, "large")

74 property, which identifies the player. The function retrieves the embed code for the currently loaded video, starts to play the video, and displays the embed code in the page element that has an

loadVideoById("bHQqvYy5KYo", 5, "large")

0 value of

loadVideoById("bHQqvYy5KYo", 5, "large")

82.

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

7

player.cueVideoById({videoId:String,

                 startSeconds:Number,
                 endSeconds:Number}):Void
3 This event fires whenever the player's state changes. The

loadVideoById("bHQqvYy5KYo", 5, "large")

75 property of the event object that the API passes to your event listener function will specify an integer that corresponds to the new player state. Possible values are:

  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 22 (unstarted)
  • var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36 (ended)
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    2 (playing)
  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 05 (paused)
  • loadVideoById("bHQqvYy5KYo", 5, "large") 36 (buffering)
  • player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3 (video cued). When the player first loads a video, it will broadcast an

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

21 (

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

  1. event. When a video is cued and ready to play, the player will broadcast a

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void
2 (

player.cueVideoByUrl(mediaContentUrl:String,

                 startSeconds:Number):Void

  1. event. In your code, you can specify the integer values or you can use one of the following namespaced variables:
  2. loadVideoById("bHQqvYy5KYo", 5, "large") 95
  3. loadVideoById("bHQqvYy5KYo", 5, "large") 96
  4. loadVideoById("bHQqvYy5KYo", 5, "large") 97
  5. loadVideoById("bHQqvYy5KYo", 5, "large") 98
  6. loadVideoById("bHQqvYy5KYo", 5, "large") 99

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
00 This event fires whenever the video playback quality changes. It might signal a change in the viewer's playback environment. See the YouTube Help Center for more information about factors that affect playback conditions or that might cause the event to fire.

The

loadVideoById("bHQqvYy5KYo", 5, "large")

75 property value of the event object that the API passes to the event listener function will be a string that identifies the new playback quality. Possible values are:

  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    02
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    03
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    04
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    05
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    06
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    07

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
08 This event fires whenever the video playback rate changes. For example, if you call the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
09 function, this event will fire if the playback rate actually changes. Your application should respond to the event and should not assume that the playback rate will automatically change when the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
09 function is called. Similarly, your code should not assume that the video playback rate will only change as a result of an explicit call to

loadVideoById("bHQqvYy5KYo", 5, "large")

08.

The

loadVideoById("bHQqvYy5KYo", 5, "large")

75 property value of the event object that the API passes to the event listener function will be a number that identifies the new playback rate. The

loadVideoById("bHQqvYy5KYo", 5, "large")

09 method returns a list of the valid playback rates for the currently cued or playing video.

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
14 This event fires if an error occurs in the player. The API will pass an

loadVideoById("bHQqvYy5KYo", 5, "large")

64 object to the event listener function. That object's

loadVideoById("bHQqvYy5KYo", 5, "large")

75 property will specify an integer that identifies the type of error that occurred. Possible values are:

  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 05 – The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.
  • player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3 – The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.
  • <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 88 – The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    20 – The owner of the requested video does not allow it to be played in embedded players.
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    21 – This error is the same as loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    20. It's just a loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    20 error in disguise!

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
24 This event is fired to indicate that the player has loaded (or unloaded) a module with exposed API methods. Your application can listen for this event and then poll the player to determine which options are exposed for the recently loaded module. Your application can then retrieve or update the existing settings for those options.

The following command retrieves an array of module names for which you can set player options:

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

8 Currently, the only module that you can set options for is the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
25 module, which handles closed captioning in the player. Upon receiving an

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
24 event, your application can use the following command to determine which options can be set for the

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
25 module:

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

9 By polling the player with this command, you can confirm that the options you want to access are, indeed, accessible. The following commands retrieve and update module options:

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

0 The table below lists the options that the API supports: Module Option Description captions fontSize This option adjusts the font size of the captions displayed in the player.

Valid values are

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

22,

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36,

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
2,

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

05, and

loadVideoById("bHQqvYy5KYo", 5, "large")

36. The default size is

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

36, and the smallest size is

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

22. Setting this option to an integer below

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

22 will cause the smallest caption size to display, while setting this option to an integer above

loadVideoById("bHQqvYy5KYo", 5, "large")

36 will cause the largest caption size to display.

captions reload This option reloads the closed caption data for the video that is playing. The value will be

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
37 if you retrieve the option's value. Set the value to

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

30 to reload the closed caption data.

loadVideoById("bHQqvYy5KYo", 5, "large")

77 This event fires any time the browser blocks autoplay or scripted video playback features, collectively referred to as "autoplay". This includes playback attempted with any of the following player APIs:

  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    40 parameter
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    41 function
  • player.loadVideoById(videoId:String,
                     startSeconds:Number):Void  
    
    9 function
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    43 function
  • loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    44 function Most browsers have policies that can block autoplay in desktop, mobile, and other environments if certain conditions are true. Instances where this policy may be triggered include unmuted playback without user interaction, or when a Permissions Policy to permit autoplay on a cross-origin iframe has not been set.

For complete details, refer to browser-specific policies (, Google Chrome, Mozilla Firefox) and Mozilla's autoplay guide.

Examples

Creating

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
5 objects

  • Example 1: Use API with existing <iframe> In this example, an loadVideoById("bHQqvYy5KYo", 5, "large") 1 element on the page already defines the player with which the API will be used. Note that either the player's player.loadVideoById(videoId:String,
                     startSeconds:Number):Void  
    
    3 URL must set the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    48 parameter to loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    2 or the loadVideoById("bHQqvYy5KYo", 5, "large") 1 element's loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    51 attribute must be set to <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>
  • The loadVideoById("bHQqvYy5KYo", 5, "large") 9 function changes the color of the border around the player to orange when the player is ready. The loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    1 function then changes the color of the border around the player based on the current player status. For example, the color is green when the player is playing, red when paused, blue when buffering, and so forth. This example uses the following code: <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 1
  • Example 2: Loud playback This example creates a 1280px by 720px video player. The event listener for the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    0 event then calls the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    56 function to adjust the volume to the highest setting. <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 2
  • Example 3: This example sets player parameters to automatically play the video when it loads and to hide the video player's controls. It also adds event listeners for several events that the API broadcasts. <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 3

Controlling 360° videos

This example uses the following code:

<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

4

Revision history

November 20, 2023

The new

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
57 event API is now available. This event notifies your application if the browser blocks autoplay or scripted playback. Verification of autoplay success or failure is an for HTMLMediaElements, and the

loadVideoById("bHQqvYy5KYo", 5, "large")

77 event now provides similar functionality for the IFrame Player API.

April 27, 2021

The and sections have been updated to include examples of using a

player.cueVideoById(videoId:String,

                startSeconds:Number):Void
8 object to customize the player.

October 13, 2020

Note: This is a deprecation announcement for the embedded player functionality that lets you configure the player to load search results. This announcement affects the IFrame Player API's ,

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
60 and

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
41.

This change will become effective on or after 15 November 2020. After that time, calls to the

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

27 or

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

28 functions that set the

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

45 property to

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

48 will generate a

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
66 response code, such as

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
67 (

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});

  1. or

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
69 (

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
70). This change also affects the

var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {

height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
  'playsinline': 1
},
events: {
  'onReady': onPlayerReady,
  'onStateChange': onPlayerStateChange
}
}); }

50 property for those functions as that property no longer supports the ability to specify a search query.

As an alternative, you can use the YouTube Data API's

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
72 method to retrieve search results and then load selected videos in the player.

October 24, 2019

The documentation has been updated to reflect the fact that the API no longer supports functions for setting or retrieving playback quality. As explained in this YouTube Help Center article, to give you the best viewing experience, YouTube adjusts the quality of your video stream based on your viewing conditions.

The changes explained below have been in effect for more than one year. This update merely aligns the documentation with current functionality:

  • The loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    73, loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    74, and loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    75 functions are no longer supported. In particular, calls to loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    74 will be no-op functions, meaning they will not actually have any impact on the viewer's playback experience.
  • The queueing functions for videos and playlists loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    77, player.loadVideoById(videoId:String,
                     startSeconds:Number):Void  
    
    9, etc.
    no longer support the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    79 argument. Similarly, if you call those functions using object syntax, the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    79 field is no longer supported. If loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    79 is specified, it will be ignored when the request is handled. It will not generate any warnings or errors.
  • The loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    82 event is still supported and might signal a change in the viewer's playback environment. See the Help Center article referenced above for more information about factors that affect playback conditions or that might cause the event to fire.

May 16, 2018

The API now supports features that allow users (or embedders) to :

  • The

    loadVideoById({'videoId': 'bHQqvYy5KYo',

               'startSeconds': 5,  
               'endSeconds': 60});  
    
    83 function retrieves the current orientation for the video playback. The orientation includes the following data:

    • yaw - represents the horizontal angle of the view in degrees, which reflects the extent to which the user turns the view to face further left or right
    • pitch - represents the vertical angle of the view in degrees, which reflects the extent to which the user adjusts the view to look up or down
    • roll - represents the rotational angle (clockwise or counterclockwise) of the view in degrees.
    • fov - represents the field-of-view of the view in degrees, which reflects the extent to which the user zooms in or out on the video.
  • The loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    84 function modifies the view to match the submitted property values. In addition to the orientation values described above, this function supports a Boolean field that indicates whether the IFrame embed should respond to loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    85 on supported mobile devices.

This demonstrates and lets you test these new features.

June 19, 2017

This update contains the following changes:

  • Documentation for the YouTube Flash Player API and YouTube JavaScript Player API has been removed and redirected to this document. The deprecation announcement for the Flash and JavaScript players was made on January 27, 2015. If you haven't done so already, please migrate your applications to use IFrame embeds and the IFrame Player API.

August 11, 2016

This update contains the following changes:

  • The newly published YouTube API Services Terms of Service ("the Updated Terms"), discussed in detail on the YouTube Engineering and Developers Blog, provides a rich set of updates to the current Terms of Service. In addition to the Updated Terms, which will go into effect as of February 10, 2017, this update includes several supporting documents to help explain the policies that developers must follow. The full set of new documents is described in the revision history for the Updated Terms. In addition, future changes to the Updated Terms or to those supporting documents will also be explained in that revision history. You can subscribe to an RSS feed listing changes in that revision history from a link in that document.

June 29, 2016

This update contains the following changes:

  • The documentation has been corrected to note that the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    86 method provides access to the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    25 module and not the loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    88 module.

June 24, 2016

The section has been updated to include an example that demonstrates how to use the API with an existing

loadVideoById("bHQqvYy5KYo", 5, "large")

1 element.

January 6, 2016

The

loadVideoById({'videoId': 'bHQqvYy5KYo',

           'startSeconds': 5,
           'endSeconds': 60});
90 function has been deprecated and removed from the documentation. The function no longer has any effect in the YouTube player.

December 18, 2015

European Union (EU) laws require that certain disclosures must be given to and consents obtained from end users in the EU. Therefore, for end users in the European Union, you must comply with the EU User Consent Policy. We have added a notice of this requirement in our .

April 28, 2014

This update contains the following changes:

  • The new function lets you remove a listener for a specified event.

March 25, 2014

This update contains the following changes:

  • The section has been updated to note that embedded players must have a viewport that is at least 200px by 200px. If a 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.

July 23, 2013

This update contains the following changes:

  • The now includes a video of a 2011 Google I/O presentation that discusses the iframe player.

October 31, 2012

This update contains the following changes:

  • The section has been updated to explain that you can use either argument syntax or object syntax to call all of those functions. Note that the API may support additional functionality in object syntax that the argument syntax does not support. In addition, the descriptions and examples for each of the have been updated to reflect the newly added support for object syntax. (The API's already supported object syntax.)
  • When called using object syntax, each of the supports an player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    0 property, which accepts a float/integer and specifies the time when the video should stop playing when player.loadVideoById({videoId:String,
                      startSeconds:Number,  
                      endSeconds:Number}):Void  
    
    2 is called.
  • The loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    93 method has been deprecated. The method now always returns a value of var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', {
    height: '390',  
    width: '640',  
    videoId: 'M7lc1UVf-VE',  
    playerVars: {  
      'playsinline': 1  
    },  
    events: {  
      'onReady': onPlayerReady,  
      'onStateChange': onPlayerStateChange  
    }  
    
    }); } 36.

August 22, 2012

This update contains the following changes:

  • The example in the section that demonstrates how to manually create the loadVideoById("bHQqvYy5KYo", 5, "large") 1 tag has been updated to include a closing loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    96 tag since the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 function is only called if the closing loadVideoById({'videoId': 'bHQqvYy5KYo',
               'startSeconds': 5,  
               'endSeconds': 60});  
    
    96 element is present.

August 6, 2012

This update contains the following changes:

  • The section has been expanded to list all of the supported API functions rather than linking to the JavaScript Player API Reference for that list.
  • The API supports several new functions and one new event that can be used to control the video playback speed:
    • Functions
      • loadVideoById("bHQqvYy5KYo", 5, "large")
             09 – Retrieve the supported playback rates for the cued or playing video. Note that variable playback rates are currently only supported in the HTML5 player.  
      • player.cueVideoById(videoId:String, startSeconds:Number):Void 00 – Retrieve the playback rate for the cued or playing video.
      • player.cueVideoById(videoId:String, startSeconds:Number):Void 01 – Set the playback rate for the cued or playing video.
    • Events
      • loadVideoById("bHQqvYy5KYo", 5, "large")
             07 – This event fires when the video's playback rate changes.

July 19, 2012

This update contains the following changes:

  • The new loadVideoById("bHQqvYy5KYo", 5, "large") 42 method replaces the now-deprecated loadVideoById("bHQqvYy5KYo", 5, "large") 29 and loadVideoById("bHQqvYy5KYo", 5, "large") 30 methods. The new method returns the percentage of the video that the player shows as buffered.
  • The player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    06 event may now return an error code of player.cueVideoByUrl(mediaContentUrl:String,
                     startSeconds:Number):Void  
    
    3, which indicates that the requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.
  • The section has been updated to indicate that any web page using the IFrame API must also implement the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 function. Previously, the section indicated that the required function was named player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    09. Code samples throughout the document have also been updated to use the new name. Note: To ensure that this change does not break existing implementations, both names will work. If, for some reason, your page has an <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 function and an player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    09 function, both functions will be called, and the <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe> 7 function will be called first.
  • The code sample in the section has been updated to reflect that the URL for the IFrame Player API code has changed to

    player.cueVideoById(videoId:String,

                    startSeconds:Number):Void  
    
    13. To ensure that this change does not affect existing implementations, the old URL ( player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
  • will continue to work.

July 16, 2012

This update contains the following changes:

  • The section now explains that the API supports the player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    15 and player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    16 methods. The player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    15 method sets the size in pixels of the loadVideoById("bHQqvYy5KYo", 5, "large") 1 that contains the player and the player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    16 method removes the loadVideoById("bHQqvYy5KYo", 5, "large") 1.

June 6, 2012

This update contains the following changes:

  • We have removed the player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    21 status from the IFrame Player API.
  • The section has been updated to point out that when inserting the loadVideoById("bHQqvYy5KYo", 5, "large") 1 element that will contain the YouTube player, the IFrame API replaces the element specified in the constructor for the YouTube player. This documentation change does not reflect a change in the API and is intended solely to clarify existing behavior. In addition, that section now notes that the insertion of the loadVideoById("bHQqvYy5KYo", 5, "large") 1 element could affect the layout of your page if the element being replaced has a different display style than the inserted loadVideoById("bHQqvYy5KYo", 5, "large") 1 element. By default, an loadVideoById("bHQqvYy5KYo", 5, "large") 1 displays as an player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    2 element.

March 30, 2012

This update contains the following changes:

  • The section has been updated to explain that the IFrame API supports a new method, player.cueVideoById(videoId:String,
                    startSeconds:Number):Void  
    
    27, which returns the DOM node for the IFrame embed.

March 26, 2012

This update contains the following changes:

  • The section has been updated to note the minimum player size.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2023-11-20 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]