feat: remember selected audio device when muting

This commit is contained in:
Jeremy Kahn 2022-10-31 09:16:46 -05:00
parent f219218d02
commit 931ddf267f

View File

@ -57,6 +57,9 @@ export function useRoom(
_setMessageLog(messages.slice(-messageTranscriptSizeLimit)) _setMessageLog(messages.slice(-messageTranscriptSizeLimit))
} }
const [audioDevices, setAudioDevices] = useState<MediaDeviceInfo[]>([]) const [audioDevices, setAudioDevices] = useState<MediaDeviceInfo[]>([])
const [selectedAudioDeviceId, setSelectedAudioDeviceId] = useState<
string | null
>(null)
useEffect(() => { useEffect(() => {
return () => { return () => {
@ -229,12 +232,13 @@ export function useRoom(
if (isSpeakingToRoom) { if (isSpeakingToRoom) {
if (!audioStream) { if (!audioStream) {
const newSelfStream = await navigator.mediaDevices.getUserMedia({ const newSelfStream = await navigator.mediaDevices.getUserMedia({
audio: true, audio: selectedAudioDeviceId
? { deviceId: selectedAudioDeviceId }
: true,
video: false, video: false,
}) })
peerRoom.addStream(newSelfStream) peerRoom.addStream(newSelfStream)
setAudioStream(newSelfStream) setAudioStream(newSelfStream)
} }
} else { } else {
@ -249,9 +253,18 @@ export function useRoom(
} }
} }
})() })()
}, [isSpeakingToRoom, peerAudios, peerRoom, audioStream]) }, [
isSpeakingToRoom,
peerAudios,
peerRoom,
audioStream,
selectedAudioDeviceId,
])
const handleAudioDeviceSelect = async (audioDevice: MediaDeviceInfo) => { const handleAudioDeviceSelect = async (audioDevice: MediaDeviceInfo) => {
const { deviceId } = audioDevice
setSelectedAudioDeviceId(deviceId)
if (!audioStream) return if (!audioStream) return
for (const audioTrack of audioStream.getTracks()) { for (const audioTrack of audioStream.getTracks()) {
@ -263,7 +276,7 @@ export function useRoom(
const newSelfStream = await navigator.mediaDevices.getUserMedia({ const newSelfStream = await navigator.mediaDevices.getUserMedia({
audio: { audio: {
deviceId: audioDevice.deviceId, deviceId,
}, },
video: false, video: false,
}) })