Skip to content

Commit 4d3fd84

Browse files
committed
On Safari, YouTube automatic posters don't show. This is a known Safari issue
Fetch poster image from YouTube/Vimeo API and set as poster attribute. Fixes #704 and #660
1 parent fa3ae30 commit 4d3fd84

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

scripts/ableplayer-base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ var AblePlayerInstances = [];
248248
var youTubeId = $(media).data('youtube-id');
249249
if ( youTubeId !== undefined && youTubeId !== "") {
250250
this.youTubeId = this.getYouTubeId(youTubeId);
251+
if ( ! this.hasPoster ) {
252+
let poster = this.getYouTubePosterUrl(this.youTubeId,'1200');
253+
$(media).attr( 'poster', poster );
254+
this.hasPoster = true;
255+
}
251256
}
252257

253258
var youTubeDescId = $(media).data('youtube-desc-id');
@@ -267,6 +272,11 @@ var AblePlayerInstances = [];
267272
var vimeoId = $(media).data('vimeo-id');
268273
if ( vimeoId !== undefined && vimeoId !== "") {
269274
this.vimeoId = this.getVimeoId(vimeoId);
275+
if ( ! this.hasPoster ) {
276+
let poster = thisObj.getVimeoPosterUrl(this.vimeoId,'1200');
277+
$(media).attr( 'poster', poster );
278+
this.hasPoster = true;
279+
}
270280
}
271281
var vimeoDescId = $(media).data('vimeo-desc-id');
272282
if ( vimeoDescId !== undefined && vimeoDescId !== "") {

scripts/buildplayer.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
AblePlayer.prototype.injectPlayerCode = function() {
44

55
// create and inject surrounding HTML structure
6-
// If iOS:
7-
// If video:
8-
// iOS does not support any of the player's functionality
9-
// - everything plays in its own player
10-
// Therefore, AblePlayer is not loaded & all functionality is disabled
11-
// (this all determined. If this is iOS && video, this function is never called)
6+
// If iOS & video:
7+
// iOS does not support any of the player's functionality - everything plays in its own player
8+
// Therefore, AblePlayer is not loaded & all functionality is disabled
9+
// (this all determined. If this is iOS && video, this function is never called)
1210

1311
var captionsContainer;
14-
15-
// create three wrappers and wrap them around the media element.
16-
// From inner to outer:
12+
// Wrappers, from inner to outer:
1713
// $mediaContainer - contains the original media element
1814
// $ableDiv - contains the media player and all its objects (e.g., captions, controls, descriptions)
1915
// $ableWrapper - contains additional widgets (e.g., transcript window, sign window)

scripts/vimeo.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,23 @@
230230

231231
AblePlayer.prototype.getVimeoPosterUrl = function (vimeoId, width) {
232232

233-
// this is a placeholder, copied from getYouTubePosterUrl()
234-
// Vimeo doesn't seem to have anything similar,
235-
// Vimeo API for images: https://vimeo.com/api/v2/video/328769500.json
236-
// will require an unauthenticated API query.
237-
238-
// return a URL for retrieving a YouTube poster image
239-
// supported values of width: 120, 320, 480, 640
240-
241-
var url = 'https://img.youtube.com/vi/' + youTubeId;
242-
if (width == '120') {
243-
// default (small) thumbnail, 120 x 90
244-
return url + '/default.jpg';
245-
} else if (width == '320') {
246-
// medium quality thumbnail, 320 x 180
247-
return url + '/hqdefault.jpg';
248-
} else if (width == '480') {
249-
// high quality thumbnail, 480 x 360
250-
return url + '/hqdefault.jpg';
251-
} else if (width == '640') {
252-
// standard definition poster image, 640 x 480
253-
return url + '/sddefault.jpg';
254-
}
255-
return false;
233+
// Vimeo Oembed only returns a 640px width image. Hope at some point there's an alternative.
234+
var url = 'http://vimeo.com/api/oembed.json?url=https://vimeo.com/' + vimeoId, imageUrl = '';
235+
console.log( url );
236+
fetch( url ).then( response => {
237+
238+
return response.json();
239+
})
240+
.then( json => {
241+
imageUrl = json.thumbnail_url;
242+
})
243+
.catch( error => {
244+
if (thisObj.debug) {
245+
console.log( 'Vimeo API query: ' + error );
246+
}
247+
});
248+
249+
return imageUrl;
256250
};
257251

258252
AblePlayer.prototype.getVimeoId = function (url) {

0 commit comments

Comments
 (0)