Skip to content

Commit

Permalink
Fix Mic Permission
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidSumra committed Nov 22, 2024
1 parent 5c21667 commit 0c0f143
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/components/Files/AudioCaptureDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Link } from "raviger";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -26,6 +26,7 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {

const { show, onHide, onCapture, autoRecord = false } = props;
const [status, setStatus] = useState<Status | null>(null);
const mediaStreamRef = useRef<MediaStream | null>(null);
const { t } = useTranslation();

const { audioURL, resetRecording, startRecording, stopRecording } =
Expand All @@ -42,7 +43,8 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {
const handleStartRecording = () => {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(() => {
.then((stream) => {
mediaStreamRef.current = stream;
setStatus("RECORDING");
startRecording();
timer.start();
Expand All @@ -57,6 +59,7 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {

const handleStopRecording = () => {
if (status !== "RECORDING") return;
releaseMic();
setStatus("RECORDED");
stopRecording();
timer.stop();
Expand Down Expand Up @@ -101,6 +104,16 @@ export default function AudioCaptureDialog(props: AudioCaptureDialogProps) {
};
}, [show]);

const releaseMic = () => {
if (mediaStreamRef.current) {
mediaStreamRef.current.getTracks().forEach((track) => {
track.stop();
mediaStreamRef.current?.removeTrack(track);
});
mediaStreamRef.current = null; // Clear the ref
}
};

useEffect(() => {
if (autoRecord && show && status === "RECORDING") {
handleStartRecording();
Expand Down
4 changes: 2 additions & 2 deletions src/components/Files/CameraCaptureDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
variant="secondary"
onClick={() => {
setPreviewImage(null);
onHide();
onResetCapture();
onHide();
}}
className="m-2"
>
Expand Down Expand Up @@ -232,8 +232,8 @@ export default function CameraCaptureDialog(props: CameraCaptureDialogProps) {
variant="secondary"
onClick={() => {
setPreviewImage(null);
onHide();
onResetCapture();
onHide();
}}
>
{`${t("close")} ${t("camera")}`}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export default function useFileUpload(
setFiles([]);
setUploadFileNames([]);
};

const Dialogues = (
<>
<CameraCaptureDialog
Expand Down

0 comments on commit 0c0f143

Please sign in to comment.