mozmorris / react-webcam

Webcam component
https://codepen.io/mozmorris/pen/JLZdoP
MIT License
1.65k stars 281 forks source link

Unable to set the muted property in the underlying video element pt. 2 #332

Open timhwang21 opened 2 years ago

timhwang21 commented 2 years ago

Please follow the general troubleshooting steps first:

Bug reports:

Follow up to #182.

It seems the fix I filed in #303 only partially works. This isn't a problem with this library, but is due to a longstanding React bug. facebook/react#6544 describes the issue in more detail.

tl;dr: While <video muted={true}/> in React results in the proper BEHAVIOR, the muted attribute is not propagated to the actual DOM for various reasons. This can cause some cross-browser permissions related issues, most notably on iOS Safari.

Several userland fixes were proposed in the thread. IMO this one is the most reasonable. React Webcam can implement this as follows here:

    return (
      <video
        autoPlay
        src={state.src}
        muted={!audio}
        playsInline
        ref={ref => {
          this.video = ref;
+         ref.defaultMuted = !audio;
+         ref.muted = !audio;
        }}
        style={videoStyle}
        {...rest}
      />
    );

(Some other commenters recommended also setting muted, because some browsers explicitly check the DOM property rather than just the muted status.)

If this is acceptable to you I can open a PR. Cheers!

jackhardingyoti commented 1 year ago

Hey @mozmorris. Any thoughts on this solution? I can confirm that this does work.