1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 09:49:28 +02:00
Improve the autoplay
This commit is contained in:
daniel 2020-01-13 21:21:06 -03:00
parent da9411e231
commit c245e4525a

View file

@ -473,38 +473,47 @@ function inIframe() {
return true; return true;
} }
} }
var promisePlayTimeout;
var promisePlay;
function playerPlay(currentTime) { function playerPlay(currentTime) {
if (typeof player === 'undefined') { if (typeof player !== 'undefined') {
if(currentTime){ if (currentTime) {
player.currentTime(currentTime); player.currentTime(currentTime);
} }
try { try {
var promise = player.play(); console.log("playerPlay: Trying to play");
promisePlay = player.play();
if (promise !== undefined) { if (promisePlay !== undefined) {
promise.then(_ => { promisePlayTimeout = setTimeout(function () {
// Autoplay started! console.log("playerPlay: Promise is Pending, try again");
}).catch(error => { playerPlay(currentTime);
}, 1000);
console.log("playerPlay: promise found");
console.log(promisePlay);
promisePlay.then(function () {
console.log("playerPlay: Autoplay started");
clearTimeout(promisePlayTimeout);
}).catch(function (error) {
console.log("playerPlay: Autoplay was prevented, trying to mute and play");
// Show something in the UI that the video is muted // Show something in the UI that the video is muted
player.muted(true); player.muted(true);
player.play(); playerPlay(currentTime);
}); });
} else {
promisePlayTimeout = setTimeout(function () {
if (player.paused()) {
console.log("playerPlay: promise Undefined");
playerPlay(currentTime);
}
}, 1000);
} }
} catch (e) { } catch (e) {
console.log("playerPlay: We could not autoplay, trying again in 1 second");
setTimeout(function () { setTimeout(function () {
var promise = player.play(); playerPlay(currentTime);
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
}).catch(error => {
// Show something in the UI that the video is muted
player.muted(true);
player.play();
});
}
}, 1000); }, 1000);
} }
} else {
console.log("playerPlay: Player is Undefined");
} }
} }