On / Off Camera - React Native
This feature enables hosts to turn their cameras on or off to share their video stream with other hosts and audience members during the room. Only hosts (in SEND_AND_RECV mode) can broadcast their camera feed, while audience members (in RECV_ONLY mode) can view it in real time.
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.
enableCam()
-
By using the
enableCam()function of theuseRoomhook, the host can publish their video to other hosts and audience members.- You can only call this method when the host is not broadcasting video. Otherwise, it rejects with
3054ERROR_ALREADY_IN_REQUESTED_STATE.
- You can only call this method when the host is not broadcasting video. Otherwise, it rejects with
-
You can also pass a customised video track in
enableCam()by using Custom Video Track. -
A stopped or released track is refused with
ERROR_INVALID_CUSTOM_VIDEO_TRACK(3029). -
The video stream of a participant can be accessed from the
webcamStreamproperty of theuseParticipanthook. -
On Android, the SDK does not ask the user for camera access. Ask for it with
requestPermission()of theuseMediaDevicehook before the live stream joins. Without it,enableCam()rejects with3017ERROR_CAMERA_ACCESS_DENIED_OR_DISMISSED. On iOS, the SDK asks the user itself.
disableCam()
-
By using the
disableCam()function of theuseRoomhook, the host can stop publishing their video to other hosts and audience members. -
You can only call this method when the host is broadcasting video. Otherwise, it rejects with
3054ERROR_ALREADY_IN_REQUESTED_STATE.
toggleCam()
-
By using the
toggleCam()function of theuseRoomhook, the host can turn their camera off when it is on, and on when it is off. Use it for a single camera button. -
It takes the same optional custom video track as
enableCam(), and uses it only when the camera is turned on.
isCamEnabled
-
The
isCamEnabledproperty of theuseRoomhook tells whether the host is publishing video. Use it to show the state of the camera button. -
The video stream of a participant can be accessed from the
webcamStreamproperty ofuseParticipanthook.
- While a camera change is still in progress, another
enableCam(),disableCam()ortoggleCam()call rejects with3055ERROR_OPERATION_IN_PROGRESS. - Audience members (in
RECV_ONLYmode) cannot publish video. For them,enableCam()andtoggleCam()reject with3066ERROR_ACTION_NOT_SUPPORTED_IN_MODE.
Example
import { useRoom } from "@videosdk.live/react-native-sdk";
import { TouchableOpacity, Text, View } from "react-native";
const LiveStreamView = () => {
//Getting the camera methods from hook
const { enableCam, disableCam, toggleCam } = useRoom();
const handleEnableWebcam = async () => {
// Enabling camera
try {
await enableCam();
} catch (err) {
console.error("enableCam failed:", err);
}
};
const handleDisableWebcam = async () => {
// Disabling camera
try {
await disableCam();
} catch (err) {
console.error("disableCam failed:", err);
}
};
const handleToggleWebcam = async () => {
// Toggling webcam
try {
await toggleCam();
} catch (err) {
console.error("toggleCam failed:", err);
}
};
return (
<View>
<TouchableOpacity onPress={handleEnableWebcam}>
<Text>Enable Webcam</Text>
</TouchableOpacity>
<TouchableOpacity onPress={handleDisableWebcam}>
<Text>Disable Webcam</Text>
</TouchableOpacity>
<TouchableOpacity onPress={handleToggleWebcam}>
<Text>Toggle Webcam</Text>
</TouchableOpacity>
</View>
);
};
To learn, how to render video in the live stream, follow this detailed guide.
Events associated with enableCam
- Every Participant—including all the hosts and audience members will receive a callback on
onStreamEnabled()event of theuseParticipant()hook with theRTCStreamobject.
Events associated with disableCam
- Every Participant—including all the hosts and audience members will receive a callback on
onStreamDisabled()event of theuseParticipant()hook with theRTCStreamobject.
Events associated with toggleCam
- Every Participant receives
onStreamEnabled()when the camera is turned on, andonStreamDisabled()when it is turned off.
import { useParticipant } from "@videosdk.live/react-native-sdk";
import { View, Text } from "react-native";
const ParticipantView = ({ participantId }) => {
//Callback for when the participant starts a stream
function onStreamEnabled(stream) {
if(stream.kind === 'video'){
console.log("Video Stream On: onStreamEnabled", stream);
}
}
//Callback for when the participant stops a stream
function onStreamDisabled(stream) {
if(stream.kind === 'video'){
console.log("Video Stream Off: onStreamDisabled", stream);
}
}
const {
displayName,
// ...
} = useParticipant(participantId,{
onStreamEnabled,
onStreamDisabled,
// ...
});
return <View><Text>Participant View</Text></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

