You have probably come across basic, all-cookie-cutter, boxy YouTube embeds. Though your own play button, unique colors, or even a looping GIF would be the background, if you wish extra dazzle. It’s absolutely possible to generate a custom YouTube player in HTML using the YouTube IFrame API. This toolkit allows you to use outside of the YouTube basic controls. Prepare; some JavaScript modification is due.
First you have to put into your HTML text the IFrame Player API script. Before your custom scripts, pop this bit of code:
<script src=”https://www.youtube.com/iframe_api”></script>
You are now keen to set the player? Set a div to hold the magic.
<div id=”player”></div>
Let’s now go over JavaScript! The API turns onYouTubeIframeAPIReady for a worldwide global use. You build a new YT.Player object there. Here’s a beginning:
function onYouTubeIframeAPIReady() {player = new YT.Player(‘player’, {height: ‘360’, width: ‘640’, videoId: ‘dQw4w9WgXcQ’, // Replace with your video ID playerVars: {controls: 0, // Hides standard controls modestbranding: 1}, events: {‘onReady’: onPlayerReady}});}
You suddenly find yourself in cosmic control. Basic buttons enable you design your own stop and play controls:
<button onclick=”player.playVideo()”>Play</button>
<button onclick=”player.pauseVideo()”>Pause</button>
Seeking more caffeine? Sync buttons or sliders adding event listeners with the player’s state. Would like to view the current era? Check player.getCurrentTime() . Jazz it with a custom progress bar or volume slider. Actually, you could make plenty right here.
Sometimes people fall over the IFrame API; it is not a straight road to total YouTube customisation. The video will stay in an iframe, hence you cannot ignore the YouTube branding and overlay rules.
You break away from formula videos with the YouTube IFrame API. Make your player genuinely yours whether your goal is cat videos to fade in like a Broadway performance or lectures with play/pause buttons as big as your aim. A little JavaScript bit is your jet fuel; the heavens are the limit.