I'm trying to get a YouTube video to pause and play again using the YouTube API. I'm using some font awesome icon to toggleClass
when clicked between a play icon and a pause icon.
So far I can get the video to pause, but when clicking the play icon to resume playback it's not firing the event and I'm not totally sure why? I'm led to believe so far that it might have to do with onPlayerStateChange()
but I'm not sure how it check for that event outright.
Here's what I have:
$( ".playback" ).click(function() {
$(this).fadeOut("fast", function() {
$(this).toggleClass('fa-play').fadeIn("fast");
$(this).toggleClass('fa-pause').fadeIn("fast");
});
});
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player('yt-holder', {
videoId: 'BlahBlah',
width: "100%",
height: "100%",
playerVars: {
autoplay: 1,
controls: 0,
showinfo: 0,
modestbranding: 1,
loop: 1,
fs: 1,
cc_load_policy: 1,
iv_load_policy: 3,
autohide: 1,
rel:0,
playlist: 'BlahBlah',
enablejsapi: 1
},
events: {
onReady: function(e) {
e.target.mute();
}
}
});
$( ".fa-play" ).on('click', function() {
player.playVideo();
});
$( ".fa-pause" ).on('click', function() {
player.pauseVideo();
});
}
Not sure where and at what point I need to check for that player state change.
Here's a pen of what I am sorta trying to do: https://codepen.io/ultraloveninja/pen/gWmWRJ