mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Polyfill HTMLMediaElement.play
Return a `Promise` to match the new standard.
This commit is contained in:
parent
801c29d3b9
commit
270430f6a1
3 changed files with 41 additions and 21 deletions
|
@ -197,28 +197,21 @@ import { Events } from 'jellyfin-apiclient';
|
||||||
|
|
||||||
export function playWithPromise(elem, onErrorFn) {
|
export function playWithPromise(elem, onErrorFn) {
|
||||||
try {
|
try {
|
||||||
const promise = elem.play();
|
return elem.play()
|
||||||
if (promise && promise.then) {
|
.catch((e) => {
|
||||||
// Chrome now returns a promise
|
const errorName = (e.name || '').toLowerCase();
|
||||||
return promise
|
// safari uses aborterror
|
||||||
.catch((e) => {
|
if (errorName === 'notallowederror' ||
|
||||||
const errorName = (e.name || '').toLowerCase();
|
errorName === 'aborterror') {
|
||||||
// safari uses aborterror
|
// swallow this error because the user can still click the play button on the video element
|
||||||
if (errorName === 'notallowederror' ||
|
|
||||||
errorName === 'aborterror') {
|
|
||||||
// swallow this error because the user can still click the play button on the video element
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
return Promise.reject();
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
onSuccessfulPlay(elem, onErrorFn);
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
});
|
}
|
||||||
} else {
|
return Promise.reject();
|
||||||
onSuccessfulPlay(elem, onErrorFn);
|
})
|
||||||
return Promise.resolve();
|
.then(() => {
|
||||||
}
|
onSuccessfulPlay(elem, onErrorFn);
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('error calling video.play: ' + err);
|
console.error('error calling video.play: ' + err);
|
||||||
return Promise.reject();
|
return Promise.reject();
|
||||||
|
|
26
src/legacy/htmlMediaElement.js
Normal file
26
src/legacy/htmlMediaElement.js
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/**
|
||||||
|
* Polyfill for HTMLMediaElement
|
||||||
|
* - HTMLMediaElement.play
|
||||||
|
* Return a `Promise`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function (HTMLMediaElement) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const HTMLMediaElement_proto = HTMLMediaElement.prototype;
|
||||||
|
const real_play = HTMLMediaElement_proto.play;
|
||||||
|
|
||||||
|
HTMLMediaElement_proto.play = function () {
|
||||||
|
try {
|
||||||
|
const promise = real_play.apply(this, arguments);
|
||||||
|
|
||||||
|
if (typeof promise?.then === 'function') {
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
} catch (err) {
|
||||||
|
return Promise.reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}(HTMLMediaElement));
|
|
@ -31,6 +31,7 @@ import './serverNotifications';
|
||||||
import '../components/playback/playerSelectionMenu';
|
import '../components/playback/playerSelectionMenu';
|
||||||
import '../legacy/domParserTextHtml';
|
import '../legacy/domParserTextHtml';
|
||||||
import '../legacy/focusPreventScroll';
|
import '../legacy/focusPreventScroll';
|
||||||
|
import '../legacy/htmlMediaElement';
|
||||||
import '../legacy/vendorStyles';
|
import '../legacy/vendorStyles';
|
||||||
import SyncPlay from '../components/syncPlay/core';
|
import SyncPlay from '../components/syncPlay/core';
|
||||||
import { playbackManager } from '../components/playback/playbackmanager';
|
import { playbackManager } from '../components/playback/playbackmanager';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue