Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Having trouble implementing code from jsfiddle

$
0
0

This project is working peferctly fine on jsfiddle but when I try to implement on my browser the player does not work.

I believe it has to do with the Youtube iframe API resource. I'm not sure how to implement it as code, I've tried adding it as tag.src per the docs. But still not working

https://jsfiddle.net/jpreynat/e11oy0eu/

my code:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="stylesheet.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script><script src="https://www.youtube.com/player_api"></script><title>Document</title></head><body><div id="player"></div><button id="button">queue 2 more videos</button><script src="script.js"></script></body></html>
var player;var playlist = ['gGagPdT3L-s','CHekNnySAfM', 'HNBCVM4KbUM'];var previousIndex = 0;$(window).load(function(){    player = new YT.Player('player', {        height: '240',        width: '300',        playerVars : {            playlist: playlist.join(','),        },        events: {            onStateChange: function(event) {                  /*                the video has changed                seize the opportunity to update the playlist without interruption                */                if(event.data == -1) {                    // get current video index                    var index = player.getPlaylistIndex();                    // update when playlists do not match                    if(player.getPlaylist().length != playlist.length) {                        // update playlist and start playing at the proper index                        player.loadPlaylist(playlist, previousIndex+1);                    }                    /*                    keep track of the last index we got                    if videos are added while the last playlist item is playing,                    the next index will be zero and skip the new videos                    to make sure we play the proper video, we use "last index + 1"                    */                    previousIndex = index;                }            }        }    });});$(document).ready(function() {       $('#button').click(function(){        playlist.push('ZVeaEl7nMWM');        playlist.push('YRBwXHowb6I');    });});

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>