Manage Audio and Video Devices - React Native
Hosts can switch their microphone, camera or audio output during a live stream. Audience members (in RECV_ONLY mode) don't publish media, but they can still use setAudioDevice() to pick the speaker, earpiece, wired headset or Bluetooth device they listen on.
This page uses the following asynchronous methods. Refer to their API reference for the errors each Promise may reject with, and handle these rejections appropriately based on your use case.
The examples on this page load the device lists when the view mounts, so show them once the live stream is joined.
Changing Audio Output Device
getMics()
-
This method of the
useMediaDevicehook lists the audio devices that audio can be routed to, which can be shown in a list for selection. -
It returns an array of objects, each with a
deviceIdand alabelrepresenting the audio device. On Android, thedeviceIdis one of the values listed below.
setAudioDevice()
- This method of the
useRoomhook switches audio to a device fromgetMics(). Pass the device object, or one of the device kinds listed below. - It does not change whether the mic is muted.
- It returns a
Promisethat rejects when the device you pass is not connected. - Supported Audio Output Devices :
SPEAKER_PHONE- Switch audio to the device speakerEARPIECE- Switch audio to the device earpieceWIRED_HEADSET- Switch audio to the connected wired deviceBLUETOOTH- Switch audio to the connected bluetooth device
import { useState, useEffect } from "react";
import { View, TouchableOpacity, Text } from "react-native";
import { useMediaDevice, useRoom } from "@videosdk.live/react-native-sdk";
const LiveStreamView = () => {
//Creating two react states to store list of available speakers and selected speaker
const [speakers, setSpeakers] = useState([]);
const [selectedSpeaker, setSelectedSpeaker] = useState("");
//Getting the method to list the audio devices
const { getMics } = useMediaDevice();
//Getting the method to switch the audio device
const { setAudioDevice } = useRoom();
//Method to get the speakers and load in our state
const handleGetSpeakers = async () => {
try {
// For example: [{ deviceId: "SPEAKER_PHONE", label: "SPEAKER_PHONE", ... }, ...]
const devices = await getMics();
setSpeakers(devices);
} catch (err) {
console.error("getMics failed:", err);
}
};
useEffect(() => {
handleGetSpeakers();
}, []);
//Changing the speaker with the selected device
const handleChangeSpeaker = async (device) => {
try {
await setAudioDevice(device);
setSelectedSpeaker(device.deviceId);
} catch (err) {
console.error("setAudioDevice failed:", err);
}
};
return (
<View>
{speakers.map((speaker) => (
<TouchableOpacity
key={speaker.deviceId}
onPress={() => handleChangeSpeaker(speaker)}
>
<Text>
{speaker.label}
{speaker.deviceId === selectedSpeaker ? " (selected)" : ""}
</Text>
</TouchableOpacity>
))}
</View>
);
};
To refresh your device list when a headset or Bluetooth device is connected or removed, pass onAudioDeviceChanged to the useMediaDevice hook. It receives a Map of the connected devices, keyed by deviceId.
const { getMics } = useMediaDevice({
onAudioDeviceChanged: (devices) => {
setSpeakers(Array.from(devices.values()));
},
});
- On Android, while a wired headset is connected, switching to
EARPIECEkeeps the audio on the headset. - On Android 12 and later, switching to
BLUETOOTHneeds theBLUETOOTH_CONNECTpermission. Ask for it withrequestBluetoothPermission()of theuseMediaDevicehook.
Changing Camera Input Device
getCameras()
-
This method of the
useMediaDevicehook lists all available cameras, which can be shown in a list for selection. -
It returns an array of objects, each containing a
deviceId, alabeland afacingMode("front"or"back") representing the camera device.
changeCam()
-
To change the camera device of the host, you need to call this method by passing the desired device's
deviceId. -
If the camera is on, it is turned off and on again with the new camera, so every participant receives
onStreamDisabled()and thenonStreamEnabled()for the video stream. -
If the camera is off, it stays off. The next
enableCam()call uses the new camera. -
Call
changeCam()without adeviceIdto switch to the next camera in thegetCameras()list, wrapping around to the first. Use it to flip between the front and back cameras. -
If the
deviceIddoes not match any camera, the call rejects with3051ERROR_NO_WEBCAM_DEVICE_FOUND, which is also reported ononError.
Example
import { useState, useEffect } from "react";
import { View, TouchableOpacity, Text } from "react-native";
import { useMediaDevice, useRoom } from "@videosdk.live/react-native-sdk";
const LiveStreamView = () => {
//Creating two react states to store list of available cameras and selected camera
const [cameras, setCameras] = useState([]);
const [selectedCamera, setSelectedCamera] = useState("");
//Getting the method to list the cameras
const { getCameras } = useMediaDevice();
//Getting the method to change the camera
const { changeCam } = useRoom();
//Method to get the cameras and load in our state
const handleGetCameras = async () => {
try {
const devices = await getCameras();
setCameras(devices);
} catch (err) {
console.error("getCameras failed:", err);
}
};
useEffect(() => {
handleGetCameras();
}, []);
//Changing the camera with the selected deviceId
const handleChangeCamera = async (deviceId) => {
try {
await changeCam(deviceId);
setSelectedCamera(deviceId);
} catch (err) {
console.error("changeCam failed:", err);
}
};
return (
<View>
{cameras.map((camera) => (
<TouchableOpacity
key={camera.deviceId}
onPress={() => handleChangeCamera(camera.deviceId)}
>
<Text>
{camera.label}
{camera.deviceId === selectedCamera ? " (selected)" : ""}
</Text>
</TouchableOpacity>
))}
</View>
);
};
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord

