Skip to main content
Version: 3.x.x

On / Off Camera - Android

This feature enables hosts to turn their cameras on or off to share their video stream with other hosts and audience members during the live stream. 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.

enableCam()​

  • By using the enableCam() function of the Room class, the host can publish their video to other hosts and audience members.

  • Without a custom track, the camera opens with the settings the live stream 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 maxResolution and defaultCamera arguments of createRoom(): 1280x720 on the front camera by default.

  • You can also pass a customised video track, created with VideoSDK.createCameraVideoTrack(), in enableCam() by using Custom Video Track.

  • Video stream of the participant can be accessed from the onStreamEnabled event of ParticipantEventListener.

disableCam()​

  • By using disableCam() function of Room class, the host can stop publishing their video to other hosts and audience members.
note
  • enableCam() and disableCam() are suspend functions. Call them from a coroutine, such as lifecycleScope.launch (from androidx.lifecycle:lifecycle-runtime-ktx).
  • They throw VideoSDKException with 3054 (ERROR_ALREADY_IN_REQUESTED_STATE) when the camera is already in the requested state, and with 3055 (ERROR_OPERATION_IN_PROGRESS) while another camera toggle is still running. enableCam() also throws the camera error when the camera can't be opened.
  • Only SEND_AND_RECV mode can publish video. Called by an audience member (RECV_ONLY mode), they throw VideoSDKException with 3066 (ERROR_ACTION_NOT_SUPPORTED_IN_MODE).
  • Called before the live stream is joined, or while it is reconnecting, they do nothing and report 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or 3065 (ERROR_ROOM_RECONNECTING) on onError instead of throwing.
  • liveStream.isCamEnabled tells you whether the camera is on.

Example​

btnWebcam!!.setOnClickListener {
lifecycleScope.launch {
try {
if (liveStream!!.isCamEnabled) {
// Disabling camera
liveStream!!.disableCam()
} else {
// Enabling camera
liveStream!!.enableCam()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Camera toggle failed: ${e.code} ${e.name}")
}
}
}
important

To learn how to render video in the live stream, follow this guide.

Events associated with enableCam​

Events associated with disableCam​

liveStream!!.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 live stream, you need to add permission in app/src/main/AndroidManifest.xml after </application>.
AndroidManifest.xml
<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 LiveStreamActivity : AppCompatActivity() {
private val PERMISSION_REQUEST_CODE: Int = 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_live_stream)

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()
}
}
}
}
important

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 live stream screen. Without it, VideoSDK reacts to every activity in your app, not only to your live stream screen. On Android 15 and later, a backgrounded app also loses its network, so the live stream 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