From 26b3f789206addd7b030539efca1ac00144e2802 Mon Sep 17 00:00:00 2001 From: maia tillie arson crimew Date: Tue, 23 Nov 2021 02:17:23 +0100 Subject: [PATCH] ignore media button events while controls are hidden this prevents local pausing (while we already stopped syncing local events to remote with controls hidden in an earlier commit) --- frontend/lib/video.mjs | 34 ++++++++++++++++++++++++++++++++++ frontend/lib/watch-session.mjs | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/frontend/lib/video.mjs b/frontend/lib/video.mjs index e4c5d4a..669ca84 100644 --- a/frontend/lib/video.mjs +++ b/frontend/lib/video.mjs @@ -28,6 +28,40 @@ const createVideoElement = (videoUrl, subtitles) => { video.appendChild(track); } + // watch for attribute changes on the video object to detect hiding/showing of controls + // as far as i can tell this is the least hacky solutions to get control visibility change events + const observer = new MutationObserver(async (mutations) => { + for (const mutation of mutations) { + if (mutation.attributeName == "controls") { + if (video.controls) { + // enable media button support + navigator.mediaSession.setActionHandler("play", null); + navigator.mediaSession.setActionHandler("pause", null); + navigator.mediaSession.setActionHandler("stop", null); + navigator.mediaSession.setActionHandler("seekbackward", null); + navigator.mediaSession.setActionHandler("seekforward", null); + navigator.mediaSession.setActionHandler("seekto", null); + navigator.mediaSession.setActionHandler("previoustrack", null); + navigator.mediaSession.setActionHandler("nexttrack", null); + navigator.mediaSession.setActionHandler("skipad", null); + } else { + // disable media button support by ignoring the events + navigator.mediaSession.setActionHandler("play", () => {}); + navigator.mediaSession.setActionHandler("pause", () => {}); + navigator.mediaSession.setActionHandler("stop", () => {}); + navigator.mediaSession.setActionHandler("seekbackward", () => {}); + navigator.mediaSession.setActionHandler("seekforward", () => {}); + navigator.mediaSession.setActionHandler("seekto", () => {}); + navigator.mediaSession.setActionHandler("previoustrack", () => {}); + navigator.mediaSession.setActionHandler("nexttrack", () => {}); + navigator.mediaSession.setActionHandler("skipad", () => {}); + } + return; + } + } + }); + observer.observe(video, { attributes: true }); + return video; }; diff --git a/frontend/lib/watch-session.mjs b/frontend/lib/watch-session.mjs index 87e296e..404990a 100644 --- a/frontend/lib/watch-session.mjs +++ b/frontend/lib/watch-session.mjs @@ -1,4 +1,4 @@ -import { setupVideo } from "./video.mjs?v=2"; +import { setupVideo } from "./video.mjs?v=e"; import { setupChat, logEventToChat } from "./chat.mjs?v=2"; /**