From 3f0bcd95a40c3c2ab33e7de2e656c67bd99971bb Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Wed, 11 Jul 2018 12:16:17 +0200 Subject: [PATCH] MOBILE-2477 core: Pause Youtube videos in Android when screen is locked --- src/app/app.component.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 8988711b5..02a7049cf 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -141,5 +141,29 @@ export class MoodleMobileApp implements OnInit { loadCustomStrings(); } }); + + // Pause Youtube videos in Android when app is put in background or screen is locked. + this.platform.pause.subscribe(() => { + if (!this.platform.is('android')) { + return; + } + + const pauseVideos = (window: Window): void => { + // Search videos in iframes recursively. + for (let i = 0; i < window.length; i++) { + pauseVideos(window[i]); + } + + if (window.location.hostname.match(/^www\.youtube(-nocookie)?\.com$/)) { + // Embedded Youtube video, pause it. + const videos = window.document.querySelectorAll('video'); + for (let i = 0; i < videos.length; i++) { + videos[i].pause(); + } + } + }; + + pauseVideos(window); + }); } }