From 76c7205f63d43d1a785788ffcc9478f50a80bfb1 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Mon, 14 Mar 2022 12:14:01 +0100 Subject: [PATCH 1/2] Stop calling pause() before play promise resolved --- scripts/html5.js | 52 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/scripts/html5.js b/scripts/html5.js index 8c51bc55..46563fcb 100644 --- a/scripts/html5.js +++ b/scripts/html5.js @@ -417,20 +417,33 @@ H5P.VideoHtml5 = (function ($) { self.seek(currentTimeBeforeChangingQuality); } - // Always play to get image. - video.play(); + // Callback to resolve play + var resolvePlaying = function () { + if (stateBeforeChangingQuality !== H5P.Video.PLAYING) { + // Do not resume playing + video.pause(); + } - if (stateBeforeChangingQuality !== H5P.Video.PLAYING) { - // Do not resume playing - video.pause(); - } + // Done changing quality + stateBeforeChangingQuality = undefined; - // Done changing quality - stateBeforeChangingQuality = undefined; + // Remove any errors + if ($error.is(':visible')) { + $error.remove(); + } + }; - // Remove any errors - if ($error.is(':visible')) { - $error.remove(); + // Always play to get image. + var playPromise = video.play(); + if (playPromise !== undefined) { + playPromise + .then(resolvePlaying) + .catch(function (error) { + resolvePlaying(); // Fallback + }); + } + else { + resolvePlaying(); // Fallback } }; video.addEventListener('loadedmetadata', loaded, false); @@ -490,8 +503,21 @@ H5P.VideoHtml5 = (function ($) { if (lastState === undefined) { // Make sure we always play before we seek to get an image. // If not iOS devices will reset currentTime when pressing play. - video.play(); - video.pause(); + + // Always play to get image. + var playPromise = video.play(); + if (playPromise !== undefined) { + playPromise + .then(function () { + video.pause(); + }) + .catch(function (error) { + video.pause(); // Fallback + }); + } + else { + video.pause(); // Fallback + } } video.currentTime = time; From fdc00dbae00aabd7e6149ed40f764ba8edb5bb04 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Thu, 2 Mar 2023 13:19:07 +0100 Subject: [PATCH 2/2] Simplify promises --- scripts/html5.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/scripts/html5.js b/scripts/html5.js index 46563fcb..adc9417d 100644 --- a/scripts/html5.js +++ b/scripts/html5.js @@ -436,11 +436,9 @@ H5P.VideoHtml5 = (function ($) { // Always play to get image. var playPromise = video.play(); if (playPromise !== undefined) { - playPromise - .then(resolvePlaying) - .catch(function (error) { - resolvePlaying(); // Fallback - }); + playPromise.finally(() => { + resolvePlaying(); + }); } else { resolvePlaying(); // Fallback @@ -507,13 +505,9 @@ H5P.VideoHtml5 = (function ($) { // Always play to get image. var playPromise = video.play(); if (playPromise !== undefined) { - playPromise - .then(function () { - video.pause(); - }) - .catch(function (error) { - video.pause(); // Fallback - }); + playPromise.finally(() => { + video.pause(); + }); } else { video.pause(); // Fallback