I can do this: https://jsfiddle.net/gjxzq8se/1/
function removePlayer() {
videoPlayer.players.forEach(function(player) {
const playerElement = player.getIframe();
if (playerElement) {
const playerClass = playerElement.className;
console.log("Destroying player with class: " + playerClass);
player.destroy();
}
});
console.log("playerRemoved");
}
return {
addPlayer,
players,
removePlayer
};
}());
inside here:
const videoPlayer = (function makeVideoPlayer() {
// videoPlayer code here...
})();
or I can add this: https://jsfiddle.net/azs8eph4/4/
const playerManager = (function makePlayerManager() {
function removePlayer() {
videoPlayer.players.forEach(function(player) {
const playerElement = player.getIframe();
if (playerElement) {
const playerClass = playerElement.className;
console.log("Destroying player with class: " + playerClass);
player.destroy();
}
});
console.log("playerRemoved");
}
return {
removePlayer
};
}());
Somewhere in here:
const videoPlayer = (function makeVideoPlayer() {
// videoPlayer code here...
})();
const managePlayer = (function makeManagePlayer() {
// managePlayer code here...
})();
const loadPlayer = (function uiLoadPlayer() {
// loadPlayer code here...
})();
More editing: https://jsfiddle.net/9ucrjb0g/3/
I went with the first option.
Not buggy at all.
Removes duplication well.
function destroyPlayers() {
videoPlayer.players.forEach(function(player) {
const playerElement = player.getIframe();
if (playerElement) {
const playerClass = playerElement.className;
console.log("Destroying player with class: " + playerClass);
player.destroy();
}
});
console.log("playerRemoved");
}
return {
addPlayer,
destroyPlayers,
players
};
}());
function removePlayer() {
videoPlayer.destroyPlayers();
}
function resetPage() {
hideContainer(".video-containerA");
showContainer(".modalA");
removePlayer();
Full function code:
const videoPlayer = (function makeVideoPlayer() {
const players = [];
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function shufflePlaylist(player, shuffle) {
player.setShuffle(shuffle);
player.playVideoAt(0);
player.stopVideo();
}
function onPlayerReady(event) {
const player = event.target;
player.setVolume(100);
const isShuffle = player.getIframe().dataset.shuffle === "true";
shufflePlaylist(player, isShuffle);
}
function onPlayerStateChange(event) {
const eventPlayer = event.target;
if (event.data === 1) {
players.forEach(function pauseOtherVideos(player) {
const hasIframe = player.getIframe();
const isDifferentPlayer = player !== eventPlayer;
const isPlaying = player.getPlayerState() === 1;
if (hasIframe && isDifferentPlayer && isPlaying) {
player.pauseVideo();
console.log("pause");
}
});
}
}
function addPlayer(video, playerOptions) {
let id = video.dataset.id;
playerOptions.playerVars = playerOptions.playerVars || {};
if (playerOptions.listType && playerOptions.list) {
playerOptions.playerVars.listType = playerOptions.listType;
playerOptions.playerVars.list = playerOptions.list;
} else if (id && id.startsWith("PL")) {
playerOptions.playerVars.listType = "playlist";
playerOptions.playerVars.list = id;
}
if (Array.isArray(playerOptions.videoId)) {
const randomNumber = Math.random() * playerOptions.videoId.length;
const randomIndex = Math.floor(randomNumber);
playerOptions.videoId = playerOptions.videoId[randomIndex];
} else if (!playerOptions.videoId && id) {
playerOptions.videoId = id;
}
video.dataset.shuffle = playerOptions.shuffle;
playerOptions.events = playerOptions.events || {};
playerOptions.events.onReady = onPlayerReady;
playerOptions.events.onStateChange = onPlayerStateChange;
players.push(new YT.Player(video, playerOptions));
}
function destroyPlayers() {
videoPlayer.players.forEach(function(player) {
const playerElement = player.getIframe();
if (playerElement) {
const playerClass = playerElement.className;
console.log("Destroying player with class: " + playerClass);
player.destroy();
}
});
console.log("playerRemoved");
}
return {
addPlayer,
destroyPlayers,
players
};
}());
Should I have done this instead?
const playerManager = (function makePlayerManager() {
function removePlayer() {
videoPlayer.players.forEach(function(player) {
const playerElement = player.getIframe();
if (playerElement) {
const playerClass = playerElement.className;
console.log("Destroying player with class: " + playerClass);
player.destroy();
}
});
console.log("playerRemoved");
}
return {
removePlayer
};
}());