|
137 | 137 |
|
138 | 138 | var youTubeMessageListenerExists = false; |
139 | 139 | 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 ;-) |
140 | 142 | var youTubeLastUrlRequest = ""; |
141 | 143 |
|
142 | 144 | var cardBody = document.getElementById("card-body"); |
|
188 | 190 | } |
189 | 191 |
|
190 | 192 | function handleMediaRequest(ev){ |
191 | | - //console.error("Media request", ev.mediaRequest); //DEBUG |
| 193 | + //console.error("Media request", ev.mediaRequest, ev.autoplay); //DEBUG |
192 | 194 | var mr = ev.mediaRequest; |
193 | 195 | /* type: music: |
194 | 196 | { |
|
293 | 295 | if (data && data.id){ |
294 | 296 | if (data.event == 'onReady'){ |
295 | 297 | //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 | + } |
296 | 309 |
|
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); |
312 | 323 | } |
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 | + } |
323 | 345 | } |
324 | 346 | } |
325 | 347 | } |
326 | 348 | } |
| 349 | + var autoplayTimer = undefined; |
327 | 350 |
|
328 | 351 | function youTubePlayerControls(cmd){ |
329 | 352 | //reset some stuff first |
|
0 commit comments