Rewrite ui.setPoster to check that images arent broken or youtube fallback images. Only show poster element when valid

This commit is contained in:
Albin Larsson
2018-05-15 05:16:06 +02:00
parent 90d5b48845
commit 16c3a7d9e5
6 changed files with 42 additions and 21 deletions

View File

@ -162,7 +162,13 @@ const youtube = {
player.media = utils.replaceElement(container, player.media);
// Set poster image
player.media.setAttribute('poster', utils.format(player.config.urls.youtube.poster, videoId));
const posterSrc = format => `https://img.youtube.com/vi/${videoId}/${format}default.jpg`;
// Check thumbnail images in order of quality, but reject fallback thumbnails (120px wide)
utils.loadImage(posterSrc('maxres'), 121) // Higest quality and unpadded
.catch(() => utils.loadImage(posterSrc('sd'), 121)) // 480p padded 4:3
.catch(() => utils.loadImage(posterSrc('hq'))) // 360p padded 4:3. Always exists
.then(image => ui.setPoster.call(player, image.src));
// Setup instance
// https://developers.google.com/youtube/iframe_api_reference