Code Snippets Series #1 YouTube Player API

Code Snippets Series #1 YouTube Player API

Hi here I am again with quick tips & tricks blog. This is the series #1 of my quick Code Snippets Series. In this series I’ll provide you source codes which make your coding life lot easier. Just copy and paste the codes to your file and enjoy.

The YouTube Player API:

The YouTube Player API lets you embed the YouTube video player on your website and then you can control the player using JavaScript functions.

Step 1: Create a <div> or any html tag as a placeholder for YouTube video
<div id=”ytplayer“></div>
Step 2:
[CODE]
<script>
var yid=”bHQqvYy5KYo”; //YouTube Video ID
// Load the IFrame Player API code asynchronously.
var tag = document.createElement(‘script’);
tag.src = “https://www.youtube.com/player_api”;
var firstScriptTag = document.getElementsByTagName(‘script’)[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Replace the ‘ytplayer‘ element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player(ytplayer‘, {
height: ‘390’,
width: ‘640’,
videoId: yid
});
}
</script>
[/CODE]

Leave a comment

Your email address will not be published. Required fields are marked *