Audio Output and Devices
Devices are listed and changed with the methods of Device management, micDevices, speakerDevices, changeMicDevice and changeSpeakerDevice. What a phone changes is what those lists hold.
Loudspeaker or earpiece
A phone has one decision to make about output: whether the voice comes out loud or against the ear. The two routes are what speakerDevices holds here, so the same code that picks an output in a browser works.
import { EARPIECE_DEVICE, Micdrop, SPEAKER_DEVICE,} from '@micdrop/react-native'
await Micdrop.changeSpeakerDevice(EARPIECE_DEVICE)await Micdrop.changeSpeakerDevice(SPEAKER_DEVICE)The current one is in the state:
const state = useMicdropState()const onEarpiece = state.speakerDeviceId === EARPIECE_DEVICE
const handlePress = () => Micdrop.changeSpeakerDevice(onEarpiece ? SPEAKER_DEVICE : EARPIECE_DEVICE)Headphones and Bluetooth are handled by the operating system: plugging them in moves the call over, whatever was chosen here.
The audio played here is PCM streamed by the server, which is one of two ways to get a voice out of a React Native app.
Choosing the microphone
The inputs the OS reports are in the state, each with an id to hand back and a label to show, and the call can be moved to another one without interrupting it:
const state = useMicdropState()
state.micDevices.map((device) => ( <Button key={device.id} title={device.label} onPress={() => Micdrop.changeMicDevice(device.id)} />))The list is filled once the call has started, since the audio session has to be active for the system to answer.
Echo
The audio session is configured as a voice call, which is what tells iOS and Android to apply echo cancellation. Without it, the microphone would hear the assistant and the call would interrupt itself.
On a device where cancellation is weak, turn interruption off so the microphone is ignored while the assistant speaks:
await Micdrop.start({ url: 'wss://example.com/call', disableInterruption: true,})