|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { Platform } from 'react-native'; |
| 3 | + |
| 4 | +import { |
| 5 | + AudioOutputDevice, |
| 6 | + AudioOutputDeviceType, |
| 7 | + AudioSessionMode, |
| 8 | +} from '../MembraneWebRTC.types'; |
| 9 | +import MembraneWebRTCModule from '../MembraneWebRTCModule'; |
| 10 | +import { ReceivableEvents, eventEmitter } from '../common/eventEmitter'; |
| 11 | + |
| 12 | +/** |
| 13 | + * This hook manages audio settings. |
| 14 | + */ |
| 15 | +export function useAudioSettings() { |
| 16 | + const [selectedAudioOutputDevice, setSelectedAudioOutputDevice] = |
| 17 | + useState<AudioOutputDevice | null>(null); |
| 18 | + const [availableDevices, setAvailableDevices] = useState<AudioOutputDevice[]>( |
| 19 | + [] |
| 20 | + ); |
| 21 | + |
| 22 | + type onAudioDeviceEvent = { |
| 23 | + AudioDeviceUpdate: { |
| 24 | + selectedDevice: AudioOutputDevice; |
| 25 | + availableDevices: AudioOutputDevice[]; |
| 26 | + }; |
| 27 | + }; |
| 28 | + |
| 29 | + const onAudioDevice = useCallback((event: onAudioDeviceEvent) => { |
| 30 | + setSelectedAudioOutputDevice(event.AudioDeviceUpdate.selectedDevice); |
| 31 | + setAvailableDevices(event.AudioDeviceUpdate.availableDevices); |
| 32 | + }, []); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const eventListener = eventEmitter.addListener( |
| 36 | + ReceivableEvents.AudioDeviceUpdate, |
| 37 | + onAudioDevice |
| 38 | + ); |
| 39 | + MembraneWebRTCModule.startAudioSwitcher(); |
| 40 | + return () => { |
| 41 | + eventListener.remove(); |
| 42 | + if (Platform.OS === 'android') { |
| 43 | + MembraneWebRTCModule.stopAudioSwitcher(); |
| 44 | + } |
| 45 | + }; |
| 46 | + }, []); |
| 47 | + |
| 48 | + /** |
| 49 | + * [Android only] selects output audio device. |
| 50 | + * For detecting and selecting bluettoth devices make sure you have the BLUETOOTH_CONNECT permission. |
| 51 | + */ |
| 52 | + const selectOutputAudioDevice = useCallback( |
| 53 | + async (device: AudioOutputDeviceType) => { |
| 54 | + if (Platform.OS === 'ios') { |
| 55 | + throw Error( |
| 56 | + 'selectOutputAudioDevice function is supported only on Android. ' + |
| 57 | + 'To select an output audio device on iOS use selectAudioSessionMode or showAudioRoutePicker functions' |
| 58 | + ); |
| 59 | + } |
| 60 | + await MembraneWebRTCModule.setOutputAudioDevice(device); |
| 61 | + }, |
| 62 | + [] |
| 63 | + ); |
| 64 | + |
| 65 | + /** |
| 66 | + * [iOS only] selects audio session mode. For more information refer to Apple's documentation: |
| 67 | + * https://developer.apple.com/documentation/avfaudio/avaudiosession/mode/ |
| 68 | + * |
| 69 | + */ |
| 70 | + const selectAudioSessionMode = useCallback( |
| 71 | + async (audioSessionMode: AudioSessionMode) => { |
| 72 | + if (Platform.OS === 'android') { |
| 73 | + throw Error('selectAudioSessionMode function is supported only on iOS'); |
| 74 | + } |
| 75 | + await MembraneWebRTCModule.selectAudioSessionMode(audioSessionMode); |
| 76 | + }, |
| 77 | + [] |
| 78 | + ); |
| 79 | + |
| 80 | + /** |
| 81 | + * [iOS only] Shows a picker modal that allows user to select output audio device. For more |
| 82 | + * information refer to Apple's documentation: https://developer.apple.com/documentation/avkit/avroutepickerview |
| 83 | + */ |
| 84 | + const showAudioRoutePicker = useCallback(async () => { |
| 85 | + if (Platform.OS === 'android') { |
| 86 | + throw Error( |
| 87 | + 'showAudioRoutePicker function is supported only on iOS. ' + |
| 88 | + 'To select an output audio device on Android use selectOutputAudioDevice function' |
| 89 | + ); |
| 90 | + } |
| 91 | + await MembraneWebRTCModule.showAudioRoutePicker(); |
| 92 | + }, []); |
| 93 | + |
| 94 | + return { |
| 95 | + /** |
| 96 | + * currently selected output audio device |
| 97 | + */ |
| 98 | + selectedAudioOutputDevice, |
| 99 | + /** |
| 100 | + * [Android only] available audio output devices to be set |
| 101 | + */ |
| 102 | + availableDevices, |
| 103 | + selectOutputAudioDevice, |
| 104 | + selectAudioSessionMode, |
| 105 | + showAudioRoutePicker, |
| 106 | + }; |
| 107 | +} |
0 commit comments