Skip to main content
Version: 2.x.x

Switch Audio Output (BETA) - React Native

This feature will help you to switch audio output device during the session.

Get Connected Device List​

This method will help you to list down all possible connected audio devices. Call getMics() from the useMediaDevice hook. It resolves with a list of devices, each with a deviceId, label and kind. On Android, the deviceId of each device is one of the names below.

Supported Audio Output Devices :​

  • SPEAKER_PHONE - Switch audio to device speaker

  • EARPIECE - Switch audio to device earpiece

  • WIRED_HEADSET - Switch audio to connected wired device

  • BLUETOOTH - Switch audio to connected bluetooth device

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

const RoomView = () => {
const { getMics } = useMediaDevice();

const onPress = async () => {
let devices;
try {
devices = await getMics();
} catch (err) {
console.error("getMics failed:", err);
}
// On Android: [{ deviceId: "SPEAKER_PHONE", label: "SPEAKER_PHONE", kind: "audioinput" }, ...]
console.log("Devices :", devices);
};

return <>...</>;
};

Switch Audio to Another Device​

This method will help you to switch specific audio output device. Call setAudioDevice() from useRoom() with a device from getMics(), or with its deviceId. Switch only to a device that getMics() returns. Otherwise, the call rejects.

import { useMediaDevice, useRoom } from "@videosdk.live/react-native-sdk";

const RoomView = () => {
const { setAudioDevice } = useRoom();
const { getMics } = useMediaDevice();

const onPress = async () => {
try {
const devices = await getMics();
const speaker = devices.find((device) => device.deviceId === "SPEAKER_PHONE");
if (speaker) {
await setAudioDevice(speaker); // for device speaker
}

await setAudioDevice("EARPIECE"); // a deviceId works too
} catch (err) {
console.error("setAudioDevice failed:", err);
}
};

return <>...</>;
};

Listen for Device Changes​

Pass an onAudioDeviceChanged callback to the useMediaDevice hook to hear when a device is connected or removed, or the route changes. The callback receives a Map of the available audio devices, keyed by deviceId.

import { useMediaDevice } from "@videosdk.live/react-native-sdk";

const AudioRoute = () => {
useMediaDevice({
onAudioDeviceChanged: (devices) => {
console.log("Available:", devices.size);
},
});

return null;
};
note
  • Call setAudioDevice() after join(). Before that there is no room, and the call rejects.
  • On Android, EARPIECE is not available while a wired headset is connected, so the audio stays on the headset.
  • On Android 12 and later, switching to BLUETOOTH needs the Bluetooth permission. Request it with requestBluetoothPermission() from useMediaDevice().

Got a Question? Ask us on discord