From bdadea8e4da384a0de1295c628f39737084b2d31 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 24 May 2024 12:07:48 +0200 Subject: [PATCH] MOBILE-4470 external-content: Fix media reloaded when playing --- src/core/directives/external-content.ts | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/core/directives/external-content.ts b/src/core/directives/external-content.ts index 7170f208d..156c5bbc2 100644 --- a/src/core/directives/external-content.ts +++ b/src/core/directives/external-content.ts @@ -423,6 +423,11 @@ export class CoreExternalContentDirective implements AfterViewInit, OnChanges, O return; } + if (this.isPlayedMedia()) { + // Don't update the URL if it's a media that already started playing, otherwise the media will be reloaded. + return; + } + const newState = await CoreFilepool.getFileStateByUrl(site.getId(), url); if (newState === state) { return; @@ -461,6 +466,29 @@ export class CoreExternalContentDirective implements AfterViewInit, OnChanges, O } } + /** + * Check if the source affects a media element that is already playing and not ended. + * + * @returns Whether it's a played media element. + */ + protected isPlayedMedia(): boolean { + let mediaElement: HTMLVideoElement | HTMLAudioElement | null = null; + + if (this.element.tagName === 'VIDEO') { + mediaElement = this.element as HTMLVideoElement; + } else if (this.element.tagName === 'AUDIO') { + mediaElement = this.element as HTMLAudioElement; + } else if (this.element.tagName === 'SOURCE' || this.element.tagName === 'TRACK') { + mediaElement = this.element.closest('video,audio'); + } + + if (!mediaElement) { + return false; + } + + return !mediaElement.paused || (mediaElement.currentTime > 0.1 && !mediaElement.ended); + } + /** * Wait for the image to be loaded or error, and emit an event when it happens. */