Skip to content

Commit 298d4ff

Browse files
committed
Update mp-youtube.html
1 parent 3000bfd commit 298d4ff

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

www/xtensions/custom-data/widgets/mp-youtube.html

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137

138138
var youTubeMessageListenerExists = false;
139139
var youTubePlayerState = 0; //States I know: -1-unstarted, 0-ended/off, 1-playing, 2-paused, 3-buffering, 5-queued (??)
140+
var youTubePlayerWaitingForPlay = false;
141+
var youTubePlaybackQuality = ""; //this is more of a "does it load" fallback than actual relevant info ;-)
140142
var youTubeLastUrlRequest = "";
141143

142144
var cardBody = document.getElementById("card-body");
@@ -188,7 +190,7 @@
188190
}
189191

190192
function handleMediaRequest(ev){
191-
//console.error("Media request", ev.mediaRequest); //DEBUG
193+
//console.error("Media request", ev.mediaRequest, ev.autoplay); //DEBUG
192194
var mr = ev.mediaRequest;
193195
/* type: music:
194196
{
@@ -293,37 +295,58 @@
293295
if (data && data.id){
294296
if (data.event == 'onReady'){
295297
//console.error("YouTube: player ready"); //DEBUG
298+
youTubePlaybackQuality = "";
299+
if (autoplay){
300+
youTubePlayerWaitingForPlay = true;
301+
//this is an autoplay-fallback to make sure it actually starts (problems on Android)
302+
autoplayTimer = setTimeout(function(){
303+
if (youTubePlayerState != 1 && !youTubePlaybackQuality) youTubePlayerControls("play");
304+
//if (youTubePlayerState != 1 && !youTubePlaybackQuality) console.error("should play now");
305+
}, 3000);
306+
}else{
307+
youTubePlayerWaitingForPlay = false;
308+
}
296309

297-
}else if (data.event == 'infoDelivery' && data.info && data.info.playerState != undefined){
298-
youTubePlayerState = data.info.playerState;
299-
300-
if (data.info.playerState == 1){
301-
//console.error("YouTube: player start", data.info); //DEBUG
302-
var title = "YouTube";
303-
var url = youTubeLastUrlRequest;
304-
if (data.info.videoData){
305-
title = data.info.videoData.title;
306-
url = "https://www.youtube.com/embed/" + encodeURIComponent(data.info.videoData.video_id);
307-
}
308-
postState(2, {
309-
meta: {
310-
title: title,
311-
url: url
310+
}else if (data.event == 'infoDelivery' && data.info){
311+
if (data.info.playerState != undefined){
312+
youTubePlayerState = data.info.playerState;
313+
314+
if (data.info.playerState == 1){
315+
//console.error("YouTube: player start", data.info); //DEBUG
316+
youTubePlayerWaitingForPlay = false;
317+
clearTimeout(autoplayTimer);
318+
var title = "YouTube";
319+
var url = youTubeLastUrlRequest;
320+
if (data.info.videoData){
321+
title = data.info.videoData.title;
322+
url = "https://www.youtube.com/embed/" + encodeURIComponent(data.info.videoData.video_id);
312323
}
313-
});
314-
315-
}else if (data.info.playerState == 2 || data.info.playerState == 0){
316-
//console.error("YouTube: player pause", data.info); //DEBUG
317-
postState(3);
318-
319-
}/*else if (data.info.playerState == -1){
320-
console.error("YouTube: player error"); //DEBUG - is it error? its not ...
321-
postError("PlayerError", "Unknown", 4);
322-
}*/
324+
postState(2, {
325+
meta: {
326+
title: title,
327+
url: url
328+
}
329+
});
330+
331+
}else if (data.info.playerState == 2 || data.info.playerState == 0){
332+
//console.error("YouTube: player pause", data.info); //DEBUG
333+
youTubePlayerWaitingForPlay = false;
334+
clearTimeout(autoplayTimer);
335+
postState(3);
336+
337+
}/*else if (data.info.playerState == -1){
338+
console.error("YouTube: player error"); //DEBUG - is it error? its not ...
339+
postError("PlayerError", "Unknown", 4);
340+
}*/
341+
}else if (youTubePlayerWaitingForPlay && data.info.playbackQuality){
342+
youTubePlaybackQuality = data.info.playbackQuality;
343+
//console.error("YouTube: playbackQuality", youTubePlaybackQuality); //DEBUG
344+
}
323345
}
324346
}
325347
}
326348
}
349+
var autoplayTimer = undefined;
327350

328351
function youTubePlayerControls(cmd){
329352
//reset some stuff first

0 commit comments

Comments
 (0)