From 54fd98b1a92ce4719563ee2d3b95ff18e2aad24b Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sat, 15 Jan 2022 23:14:47 -0800 Subject: [PATCH] can select audio device now nicely --- contexts/audioContext.tsx | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/contexts/audioContext.tsx b/contexts/audioContext.tsx index 645ed95..b23b660 100644 --- a/contexts/audioContext.tsx +++ b/contexts/audioContext.tsx @@ -35,8 +35,6 @@ interface AudioContextInterface { const AudioContext = React.createContext(null); -const Mp3Recorder = new MicRecorder({ bitRate: 128 }); - function stopBothVideoAndAudio(stream) { stream.getTracks().forEach(function (track) { if (track.readyState == "live") { @@ -63,6 +61,10 @@ export default function AudioContextProvider({ children }) { const [isMuted, setIsMuted] = useState(false); const [isSilenceMode, setIseSilenceMode] = useState(false); + const [recorder, setRecorder] = useState( + new MicRecorder({ bitRate: 128 }) + ); + const value: AudioContextInterface = { selectedTeammate, selectTeamMember, @@ -134,6 +136,8 @@ export default function AudioContextProvider({ children }) { })(); }, [hasRecPermit]); + // first load just force check if permissions enabled + // through fake stream useEffect(() => { navigator.mediaDevices .getUserMedia({ audio: { deviceId: audioInputDeviceId } }) @@ -146,14 +150,24 @@ export default function AudioContextProvider({ children }) { }) .catch((e) => { console.log("Permission Denied"); - toast.error("Please make sure that you have connected a microphone."); + toast.error( + "Please make sure that you have connected a microphone and given permissions." + ); setHasRecPermit(false); }); + }, []); + + // set recording device + useEffect(() => { + setRecorder( + new MicRecorder({ bitRate: 128, deviceId: audioInputDeviceId }) + ); }, [audioInputDeviceId]); // change it everytime we change the input device // SECTION: recording async function startRecording() { - Mp3Recorder.start() + recorder + .start() .then(() => { toast.success("started recording"); }) @@ -164,7 +178,8 @@ export default function AudioContextProvider({ children }) { // everything to do with audio file async function stopRecording() { - Mp3Recorder.stop() + recorder + .stop() .getMp3() .then(([buffer, blob]) => { const blobURL = URL.createObjectURL(blob);