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