4828
Sr
stream-together / files
stream-together / hooks / use-microphone.ts
1.2 KB
1import AgoraRTC, { useLocalMicrophoneTrack } from "agora-rtc-react"; 2import { useEffect, useState } from "react"; 3 4import useNoiseReduction from "./use-agora-ai-noise-reduction"; 5 6export default function useMicrophone(audioStarted: boolean) { 7 const [availableMicrophones, setAvailableMicrophones] = useState< 8 { id: string; label: string }[] 9 >([]); 10 11 const [selectedMicrophoneId, setSelectedMicrophoneId] = useState(""); 12 13 useEffect(() => { 14 const getMicrophones = async () => { 15 const microphones = await AgoraRTC.getCameras(); 16 setAvailableMicrophones( 17 microphones.map((c) => ({ id: c.deviceId, label: c.label })), 18 ); 19 setSelectedMicrophoneId(microphones[0].deviceId); 20 }; 21 void getMicrophones(); 22 }, []); 23 24 const { localMicrophoneTrack } = useLocalMicrophoneTrack(audioStarted, { 25 microphoneId: selectedMicrophoneId, 26 }); 27 useNoiseReduction({ localMicrophoneTrack }); 28 29 useEffect(() => { 30 localMicrophoneTrack?.setDevice(selectedMicrophoneId); 31 }, [localMicrophoneTrack, selectedMicrophoneId]); 32 33 return { 34 localMicrophoneTrack, 35 availableMicrophones, 36 selectedMicrophoneId, 37 setSelectedMicrophoneId, 38 }; 39}