On / Off Camera - Android
Any participant can turn their camera on or off in the room with the methods below.
enableCam()
-
Call
room.enableCam()to start sending the local participant's camera to the room. Call it while the camera is off. -
Without a custom track, the camera opens with the settings the room already has: those of the last custom track you passed, on the camera in use. Before any custom track, these are the size and side set by the
maxResolutionanddefaultCameraarguments ofcreateRoom(): 1280x720 on the front camera by default. -
You can pass a custom video track, created with
VideoSDK.createCameraVideoTrack(), toenableCam(). Learn more in Custom Video Track. -
The participant's video stream arrives on the
onStreamEnabledevent ofParticipantEventListener.
disableCam()
-
Call
room.disableCam()to stop sending the local participant's camera. Call it while the camera is on. -
The camera is closed, so the system camera indicator goes off.
enableCam()anddisableCam()aresuspendfunctions. Call them from a coroutine, such aslifecycleScope.launch(fromandroidx.lifecycle:lifecycle-runtime-ktx).- They throw
VideoSDKExceptionwith3054(ERROR_ALREADY_IN_REQUESTED_STATE) when the webcam is already in the requested state, and with3055(ERROR_OPERATION_IN_PROGRESS) while another webcam toggle is still running.enableCam()also throws the camera error when the camera can't be opened. - Called before the room is joined, or while it is reconnecting, they do nothing and report
3035(ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or3065(ERROR_ROOM_RECONNECTING) ononErrorinstead of throwing. room.isCamEnabledtells you whether the webcam is on.
Example
btnWebcam!!.setOnClickListener {
lifecycleScope.launch {
try {
if (room!!.isCamEnabled) {
// Disabling camera
room!!.disableCam()
} else {
// Enabling camera
room!!.enableCam()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Webcam toggle failed: ${e.code} ${e.name}")
}
}
}
To learn how to render video in the room, follow this detailed guide.
Events associated with enableCam
- On every device in the room,
onStreamEnabled()is called on the listener of thatParticipant, with the newRTCStream. ItskindisMediaType.VIDEO.
Events associated with disableCam
- On every device in the room,
onStreamDisabled()is called on the listener of thatParticipant, with theRTCStreamthat stopped. ItskindisMediaType.VIDEO.
room!!.localParticipant.addEventListener(object : ParticipantEventListener() {
//Callback for when the participant starts a stream
override fun onStreamEnabled(stream: RTCStream) {
if (stream.kind == MediaType.VIDEO) {
Log.d("VideoSDK", "Video Stream On: onStreamEnabled $stream")
}
}
//Callback for when the participant stops a stream
override fun onStreamDisabled(stream: RTCStream) {
if (stream.kind == MediaType.VIDEO) {
Log.d("VideoSDK", "Video Stream Off: onStreamDisabled $stream")
}
}
})
Video Permissions
- To use the camera in a room, declare
CAMERAinapp/src/main/AndroidManifest.xml, inside<manifest>and outside<application>.
<uses-permission android:name="android.permission.CAMERA" />
- You need to set up a permission request that provides this access.
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
private val PERMISSION_REQUEST_CODE: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (checkPermission()) {
// . write your main code to execute, It will execute if the permission is already given.
} else {
requestPermission()
}
}
private fun checkPermission(): Boolean {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
}
private fun requestPermission() {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.CAMERA),
PERMISSION_REQUEST_CODE
)
}
override
fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
PERMISSION_REQUEST_CODE -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_SHORT).show()
// main logic
} else {
Toast.makeText(applicationContext, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
When the app goes to the background, VideoSDK turns the camera off so other apps can use it, and turns it back on when the app returns. A camera you turned off yourself stays off. Call VideoSDK.setActivityForLifeCycle(activity) with your call screen. Without it, VideoSDK reacts to every activity in your app, so opening another screen of your app also turns the camera off. On Android 15 and later, a backgrounded app also loses its network, so the call drops unless your app runs a foreground service. See Background Handling.
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

