Skip to content

Commit

Permalink
Editor client: Pause/Resume video on tab visibility change
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-legion committed Mar 25, 2022
1 parent ad7de60 commit c12432c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/lgn-web-client/frontend/src/actions/videoPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ export default function videoPlayer(
let listeners: Listener[] = [];
let lastFrameId = -1;

async function onVisibilityChange() {
switch (document.visibilityState) {
case "hidden": {
videoElement.pause();

break;
}

case "visible": {
await videoElement.play();

break;
}
}
}

function addListener(source: Source, name: string, f: () => void) {
source.addEventListener(name, f);

Expand Down Expand Up @@ -119,6 +135,8 @@ export default function videoPlayer(
}
}

document.addEventListener("visibilitychange", onVisibilityChange);

(videoElement as PushableHTMLVideoElement).push = (data) => {
const chunk = new Uint8Array(data);
const frameId =
Expand Down Expand Up @@ -153,4 +171,12 @@ export default function videoPlayer(
lastFrameId = frameId;
}
};

return {
destroy() {
document.removeEventListener("visibilitychange", onVisibilityChange);

destroy();
},
};
}

0 comments on commit c12432c

Please sign in to comment.