Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Handle visibility change on Camera preview

Ilya5dea93b680ea
diff --git a/apps/desktop/src/components/windows/Camera.tsx b/apps/desktop/src/components/windows/Camera.tsx
index fd6cf7a26..6610461d2 100644
--- a/apps/desktop/src/components/windows/Camera.tsx
+++ b/apps/desktop/src/components/windows/Camera.tsx
@@ -29,24 +29,38 @@ export const Camera = () => {
if (typeof navigator === "undefined") return;
- navigator.mediaDevices
- .getUserMedia(constraints)
- .then((stream) => {
- video.srcObject = stream;
- video.play();
- setIsLoading(false);
- })
- .catch((err) => {
- console.error(err);
- });
+ const initializeVideoStream = () => {
+ navigator.mediaDevices
+ .getUserMedia(constraints)
+ .then((stream) => {
+ video.srcObject = stream;
+ video.play();
+ setIsLoading(false);
+ })
+ .catch((err) => {
+ console.error(err);
+ });
+ }
- return () => {
+ const onVisibilityChanged = () => {
+ if (document.hidden) stop();
+ else initializeVideoStream();
+ }
+
+ const stop = () => {
if (video.srcObject) {
const stream = video.srcObject as MediaStream;
stream.getTracks().forEach((track) => {
track.stop();
});
}
+ }
+
+ document.addEventListener('visibilitychange', onVisibilityChanged);
+
+ return () => {
+ stop();
+ document.removeEventListener('visibilitychange', onVisibilityChanged);
};
}, [selectedVideoDevice]);