Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the play icon of embedded youtube videos?

Tags:

css

youtube

Am I allowed to replace the original youtube play icon on embedded youtube videos? I would place my own icon above the youtube iframe to achieve that. I know that facebook does not allow to style the like button, so that's why I am asking about youtube player customization.

like image 581
Patartics Milán Avatar asked Jan 17 '14 09:01

Patartics Milán


1 Answers

I'm not sure you can customise a YouTube embedded without using the YouTube JavaScript api.

Heres how to do it with the api:

Javascript

//youtube script
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;

onYouTubeIframeAPIReady = function () {
    player = new YT.Player('player', {
        height: '244',
        width: '434',
        videoId: 'AkyQgpqRyBY',  // youtube video id
        playerVars: {
            'autoplay': 0,
            'rel': 0,
            'showinfo': 0
        },
        events: {
            'onStateChange': onPlayerStateChange
        }
    });
}

onPlayerStateChange = function (event) {
    if (event.data == YT.PlayerState.ENDED) {
        $('.start-video').fadeIn('normal');
    }
}

$(document).on('click', '.start-video', function () {
    $(this).fadeOut('normal');
    player.playVideo();
});

HTML

<div id="player"></div>
<button class="start-video">Start Video</button>

CSS

button {
    position: absolute;
    top: 106px;
    padding: 12px;
    left: 174px;
}

I've created a Jsfiddle with a working example.

like image 187
AfromanJ Avatar answered Nov 09 '22 09:11

AfromanJ