diff --git a/src/useUserMedia/index.ts b/src/useUserMedia/index.ts index 1bb6beb..bbab32e 100644 --- a/src/useUserMedia/index.ts +++ b/src/useUserMedia/index.ts @@ -245,6 +245,14 @@ export const userMediaReducer = (state: UseUserMediaState, action: UseUserMediaA } else if (action.type === "UseUserMedia-setAudioAndVideo") { return { audio: action.audio, video: action.video }; } else if (action.type === "UseUserMedia-setMedia") { + if (action.video.restart) { + prevState?.video?.media?.track?.stop(); + } + + if (action.audio.restart) { + prevState?.audio?.media?.track?.stop(); + } + const videoMedia: Media | null = action.video.restart ? { stream: action.stream, @@ -494,20 +502,12 @@ export const useUserMediaInternal = ( if (result.type === "OK") { const stream = result.stream; - if (shouldRestartVideo) { - state?.video.media?.track?.stop(); - } - const currentVideoDeviceId = result.stream.getVideoTracks()?.[0]?.getSettings()?.deviceId; const videoInfo = currentVideoDeviceId ? getDeviceInfo(currentVideoDeviceId, state.video.devices ?? []) : null; if (videoInfo) { saveLastVideoDevice?.(videoInfo); } - if (shouldRestartAudio) { - state?.audio.media?.track?.stop(); - } - const currentAudioDeviceId = result.stream.getAudioTracks()?.[0]?.getSettings()?.deviceId; const audioInfo = currentAudioDeviceId ? getDeviceInfo(currentAudioDeviceId, state.audio.devices ?? []) : null;