-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VIDCS-3273: Fix audio output selection crash in android Waiting Room #23
Changes from 12 commits
1d2e635
b9262c8
b847d7f
8fe2e1c
467cac6
d04f57c
449f9c8
e0747b0
a01c20c
445a82c
199c7a0
ebacbbf
87abb97
43bb33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { | ||
getActiveAudioOutputDevice as getVonageActiveAudioOutputDevice, | ||
setAudioOutputDevice as setVonageAudioOutputDevice, | ||
} from '@vonage/client-sdk-video'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { getActiveAudioOutputDevice, setAudioOutputDevice } from '@vonage/client-sdk-video'; | ||
|
||
export type AudioDeviceId = string | null | undefined; | ||
|
||
export type AudioOutputContextType = { | ||
audioOutput: string | undefined | null; | ||
setAudioOutput: (deviceId: AudioDeviceId) => Promise<void>; | ||
currentAudioOutputDevice: string | undefined | null; | ||
setAudioOutputDevice: (deviceId: AudioDeviceId) => Promise<void>; | ||
Comment on lines
+1
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some light renaming/refactoring here. It was super confusing to know what |
||
}; | ||
|
||
/** | ||
|
@@ -15,26 +18,41 @@ export type AudioOutputContextType = { | |
* @returns {AudioOutputContextType} audioOutput context | ||
*/ | ||
const useAudioOutput = (): AudioOutputContextType => { | ||
const [audioOutput, setAudioOutput] = useState<AudioDeviceId>(); | ||
const [currentAudioOutputDevice, setCurrentAudioOutputDevice] = useState<AudioDeviceId>(); | ||
const { mediaDevices } = window.navigator; | ||
|
||
useEffect(() => { | ||
getActiveAudioOutputDevice().then((audioOutputDevice) => { | ||
const updateCurrentAudioOutputDevice = useCallback(() => { | ||
getVonageActiveAudioOutputDevice().then((audioOutputDevice) => { | ||
if (audioOutputDevice.deviceId) { | ||
setAudioOutput(audioOutputDevice.deviceId); | ||
setCurrentAudioOutputDevice(audioOutputDevice.deviceId); | ||
} | ||
}); | ||
}, []); | ||
|
||
const updateAudioOutput = useCallback(async (deviceId: AudioDeviceId) => { | ||
useEffect(() => { | ||
updateCurrentAudioOutputDevice(); | ||
}, [updateCurrentAudioOutputDevice]); | ||
|
||
useEffect(() => { | ||
// Add an event listener to update device list when the list changes (such as plugging or unplugging a device) | ||
mediaDevices.addEventListener('devicechange', updateCurrentAudioOutputDevice); | ||
|
||
return () => { | ||
// Remove the event listener when component unmounts | ||
mediaDevices.removeEventListener('devicechange', updateCurrentAudioOutputDevice); | ||
}; | ||
}, [mediaDevices, updateCurrentAudioOutputDevice]); | ||
Comment on lines
+36
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this event listener to update the current device when the device list changes. This keeps the UI in sync if the current device was removed |
||
|
||
const setAudioOutputDevice = useCallback(async (deviceId: AudioDeviceId) => { | ||
if (deviceId) { | ||
await setAudioOutputDevice(deviceId); | ||
setAudioOutput(deviceId); | ||
await setVonageAudioOutputDevice(deviceId); | ||
setCurrentAudioOutputDevice(deviceId); | ||
} | ||
}, []); | ||
|
||
return { | ||
audioOutput, | ||
setAudioOutput: updateAudioOutput, | ||
currentAudioOutputDevice, | ||
setAudioOutputDevice, | ||
}; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was me trying to get non-empty react types DefinitelyTyped/DefinitelyTyped#13251 (comment)