Skip to content

Commit fc3a250

Browse files
committed
fix: disable the microphone after end the call
1 parent 1a507e7 commit fc3a250

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/components/RoomScreen.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,25 @@ function useMediaControls() {
3434
}
3535
}, [localParticipant, isMuted]);
3636

37+
const disableMic = useCallback(async () => {
38+
if (isMuted || !localParticipant) {
39+
setIsMuted(true);
40+
return;
41+
}
42+
43+
try {
44+
await localParticipant.setMicrophoneEnabled(false);
45+
} catch (err) {
46+
console.error('Failed to disable microphone', err);
47+
} finally {
48+
setIsMuted(true);
49+
}
50+
}, [localParticipant, isMuted]);
51+
3752
return {
3853
isMuted,
3954
enableMic,
55+
disableMic,
4056
};
4157
}
4258

@@ -81,28 +97,33 @@ function ParticipantTile({ participant }) {
8197
}
8298

8399
function RoomContainer({ roomName, onLeave }) {
84-
const { isMuted, enableMic } = useMediaControls();
100+
const { isMuted, enableMic, disableMic } = useMediaControls();
85101

86102
const hadRemoteParticipantRef = useRef(false);
87103

88104
const remoteParticipants = useRemoteParticipants({
89105
updateOnlyOn: [RoomEvent.ParticipantConnected, RoomEvent.ParticipantDisconnected],
90106
});
91107

108+
const leaveRoom = useCallback(async () => {
109+
await disableMic();
110+
onLeave();
111+
}, [disableMic, onLeave]);
112+
92113
useEffect(() => {
93114
if (remoteParticipants.length > 0) {
94115
hadRemoteParticipantRef.current = true;
95116
return;
96117
}
97118

98119
if (hadRemoteParticipantRef.current && remoteParticipants.length === 0) {
99-
onLeave();
120+
leaveRoom();
100121
}
101-
}, [remoteParticipants.length, onLeave]);
122+
}, [remoteParticipants.length, leaveRoom]);
102123

103124
const handleClose = async (event) => {
104125
event.stopPropagation();
105-
onLeave();
126+
await leaveRoom();
106127

107128
if (remoteParticipants.length === 0) {
108129
try {

0 commit comments

Comments
 (0)