Skip to content

Commit

Permalink
Fix: 마이크 설정 정보를 저장할 필요가 없어 상태값 삭제 #49
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Sep 19, 2024
1 parent 176ada8 commit e340976
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/hooks/useAudio.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { AudioActionType, AudioContext } from "@contexts/AudioProvider";
import { SocketContext } from "@contexts/SocketProvider";
import { useEffect, useState, useContext } from "react";
import { useState, useContext } from "react";

export function useAudio() {
const [selectedDeviceId, setSelectedDeviceId] =
useState("마이크를 선택해 주세요.");
const { socket } = useContext(SocketContext);
const { audio, dispatch } = useContext(AudioContext);
const [isAudioLoading, setIsAudioLoading] = useState(false);

const getAudioInputs = async () => {
setIsAudioLoading(true);
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const excludeDefault = devices.filter(
(v) => v.deviceId !== "default" && v.deviceId !== ""
);
const inputs = excludeDefault.filter((v) => v.kind === "audioinput");
const audioInputs = devices
.filter((v) => v.deviceId !== "" && v.kind === "audioinput")
.sort((a, b) => {
if (a.label.includes("Default")) return -1;
if (b.label.includes("Default")) return 1;
return 0;
});

dispatch({ type: AudioActionType.SET_AUDIOLIST, payload: inputs });
dispatch({ type: AudioActionType.SET_AUDIOLIST, payload: audioInputs });
setTimeout(() => {
setIsAudioLoading(false);
}, 100);
Expand All @@ -36,25 +35,14 @@ export function useAudio() {
deviceIdIndex = i;
}
}
setSelectedDeviceId(audio.audioList[deviceIdIndex].deviceId);
return deviceIdIndex;
};

useEffect(() => {
// useAudio가 호출될때마다 dispatch 로직이 실행되어서 발생한 오류
if (!audio.audioList.length) {
getAudioInputs();
}
}, [socket]);

return {
stream: audio.stream,
deviceId: audio.deviceId,
audioList: audio.audioList,
isAudioLoading,
selectedDeviceId,
setSelectedDeviceId,
refresh: getAudioInputs,
findDeviceIdIndex,
getAudioInputs,
};
}

0 comments on commit e340976

Please sign in to comment.