Marco Santos
Marco Santos

Reputation: 1005

Embed youtube APi Custom button

im customizing my youtube player, but im getting a error, i already check if the problem was not wrapping in a ready function, but still getting the error "Uncaught ReferenceError: YT is not defined"

Js:

//youtube script
var tag = document.createElement('script');
tag.src = "http://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: '193',
        width: '284',
        videoId: 'jGa2J3RBPtQ',  // 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">Play</button>

Upvotes: 1

Views: 104

Answers (1)

Joseph Young
Joseph Young

Reputation: 2795

Have you tried switching to the HTTPS protocol?

Worked for me here

tag.src = "https://www.youtube.com/iframe_api";

Upvotes: 1

Related Questions