From e340976c2ae88fa33ee41f350d1a1f416377d36e Mon Sep 17 00:00:00 2001 From: luckylooky2 Date: Thu, 19 Sep 2024 19:55:54 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EB=A7=88=EC=9D=B4=ED=81=AC=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=ED=95=A0=20=ED=95=84=EC=9A=94=EA=B0=80=20=EC=97=86=EC=96=B4=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=EA=B0=92=20=EC=82=AD=EC=A0=9C=20#49?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useAudio.ts | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/hooks/useAudio.ts b/src/hooks/useAudio.ts index 46faa52..97863a8 100644 --- a/src/hooks/useAudio.ts +++ b/src/hooks/useAudio.ts @@ -1,11 +1,7 @@ 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); @@ -13,12 +9,15 @@ export function useAudio() { 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); @@ -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, }; }