Skip to content

Camera showing black/white screen when multiple tabs are open #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
imtiaz993 opened this issue Mar 8, 2025 · 2 comments
Open

Camera showing black/white screen when multiple tabs are open #583

imtiaz993 opened this issue Mar 8, 2025 · 2 comments
Labels

Comments

@imtiaz993
Copy link

imtiaz993 commented Mar 8, 2025

When using the BrowserMultiFormatReader from @zxing/browser in a React/Next.js application with multiple browser tabs open, the camera stream fails to initialize correctly in tabs. Specifically:

The camera remains locked to the first tab that accesses it.
Inactive tabs display a white screen instead of the camera feed.
The camera does not release properly when switching tabs, preventing other tabs from accessing it until the page is manually interacted with (e.g., clicking a button) or refreshed, and even then, it often requires closing the original tab.
This issue persists despite attempts to stop and restart the camera stream using reset() and managing tab visibility with visibilitychange events.

Expected behavior
When switching between tabs, the camera should be released from the inactive tab and made available to the active tab.
The

`import { useEffect, useRef, useState } from "react";
import { BrowserMultiFormatReader } from "@zxing/browser";

const ZXingScanner = ({ onScan, openCart }: any) => {
const videoRef = useRef(null);
const beepSound = new Audio("/beep.mp3");
const [cameraAccessError, setCameraAccessError] = useState(false);

useEffect(() => {
const codeReader = new BrowserMultiFormatReader();

codeReader
  .decodeFromVideoDevice("", videoRef.current, (result) => {
    if (result && !openCart) {
      onScan(result.getText(), beepSound);
    }
  })
  .catch((error) => {
    setCameraAccessError(true);
  });

}, [onScan]);

const handleReload = () => {
window.location.reload();
};

return (


{cameraAccessError && (

Camera access denied.{" "}

Reload page{" "}

or{" "}

check permissions.


)}
<video
ref={videoRef}
style={{ width: "100%", height: "100%", objectFit: "cover" }}
/>
<div
style={{
position: "absolute",
top: "50%",
left: "10px",
width: "calc(100% - 20px)",
height: "2px",
backgroundColor: "red",
}}
>


);
};

export default ZXingScanner;
`

@imtiaz993 imtiaz993 added the bug label Mar 8, 2025
@imtiaz993
Copy link
Author

@werthdavid

@werthdavid
Copy link
Member

Sorry, I don't have experience with React. I can imagine that the stop and start on the next tab is "too quick" to release the camera before it is accessed again. Can you try waiting some time after closing the camera before accessing again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants