File size: 2,505 Bytes
2ff9f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# YouTube IFrame Player API Reference (English)

Source: https://developers.google.com/youtube/iframe_api_reference?hl=en

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

## Getting Started

### Browser Requirements
The user's browser must support the HTML5 `postMessage` feature.

### Minimum Viewport Size
Embedded players must have a viewport that is at least 200px by 200px. Recommended size for 16:9 players is 480px by 270px.

## Implementation Example

```html
<!DOCTYPE html>
<html>
  <body>
    <!-- The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      // Load 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);

      // 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
          }
        });
      }

      function onPlayerReady(event) {
        event.target.playVideo();
      }

      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>
```

## Functions

### Queueing Functions
- `cueVideoById`: Loads thumbnail and prepares player.
- `loadVideoById`: Loads and plays video.
- `cuePlaylist`: Loads playlist thumbnail and prepares player.
- `loadPlaylist`: Loads and plays playlist.

### Playback Controls
- `player.playVideo()`
- `player.pauseVideo()`
- `player.stopVideo()`
- `player.seekTo(seconds, allowSeekAhead)`
- `player.nextVideo()`
- `player.previousVideo()`
- `player.mute()`
- `player.unMute()`
- `player.setVolume(volume)`
- `player.getVolume()`

### Events
- `onReady`
- `onStateChange`
- `onPlaybackQualityChange`
- `onPlaybackRateChange`
- `onError`
- `onApiChange`