Skip to content Skip to sidebar Skip to footer

Pass Youtube Iframe Api Events Onstatechange When Src Changes

So I figured I would update this with a working example. I have ditched stating the iframe tag and just used the iframe api to create an iframe and then loaded the player by id wi

Solution 1:

Rather than having buttons to play, why not cue to playlist when the player is ready? You can hold the video ID's in an array... If this isn't what you're looking for just leave a comment below and I will change things the best I can to fit.

//Array of videosvarMyVideos=["E6RGMRamAFk","IHQr0HCIN2w","CogIXrea6A4"];
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
functiononYouTubeIframeAPIReady() {
    player = newYT.Player('player', {
    events: {
     'onReady': onPlayerReady,
     'onStateChange': onPlayerStateChange
    }
  });
}
functiononPlayerReady(event) {
//Player is ready, cue the array of videos into the playlist.
player.cuePlaylist(MyVideos);
}
functiononPlayerStateChange(event) {
}

JS Fiddle - Working Demo

YouTube JavaScript Player API Reference

If you have any questions please leave a comment below and I will get back to you as soon as possible.

I hope this help. Happy coding!

Post a Comment for "Pass Youtube Iframe Api Events Onstatechange When Src Changes"