Merge pull request #347 from robintown/prevent-unmute

Prevent video elements from being mistakenly muted/unmuted
This commit is contained in:
Robin 2022-05-24 07:44:43 -04:00 committed by GitHub
commit 126bfec339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,10 +27,21 @@ export function useMediaStream(stream, audioOutputDevice, mute = false) {
); );
if (mediaRef.current) { if (mediaRef.current) {
const mediaEl = mediaRef.current;
if (stream) { if (stream) {
mediaRef.current.muted = mute; mediaEl.muted = mute;
mediaRef.current.srcObject = stream; mediaEl.srcObject = stream;
mediaRef.current.play(); mediaEl.play();
// Unmuting the tab in Safari causes all video elements to be individually
// unmuted, so we need to reset the mute state here to prevent audio loops
const onVolumeChange = () => {
mediaEl.muted = mute;
};
mediaEl.addEventListener("volumechange", onVolumeChange);
return () =>
mediaEl.removeEventListener("volumechange", onVolumeChange);
} else { } else {
mediaRef.current.srcObject = null; mediaRef.current.srcObject = null;
} }