Skip to main content
Version: 0.1.x

On / Off Camera - Android

Any participant can turn on or off his camera in the meeting using below methods.

enableWebcam()

  • By using enableWebcam() function of Meeting class, local participant can publish video to other participants.

  • You can call this method when the local participant is not broadcasting any video to others.

  • You can pass customised video track in enableWebcam() by using Custom Video Track.

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

disableWebcam()

  • By using disableWebcam() function of Meeting class, local participant can stop publish video to other participants.

  • You can call this method when the local participant is broadcasting any video to others.

Example

  btnWebcam!!.setOnClickListener {
if (webcamEnabled) {
// Disabling camera
meeting!!.disableWebcam()
} else {
// Enabling camera
meeting!!.enableWebcam()
}
webcamEnabled=!webcamEnabled
}
info

To learn, how to render video in the meeting, follow this detailed guide.

Events associated with enableWebcam

Events associated with disableWebcam

  meeting!!.localParticipant.addEventListener(object : ParticipantEventListener() {
//Callback for when the participant starts a stream
override fun onStreamEnabled(stream: Stream) {
if(stream.getKind().equals("video")){
Log.d("VideoSDK","Video Stream On: onStreamEnabled $stream");
}
}

//Callback for when the participant stops a stream
override fun onStreamDisabled(stream: Stream) {
if(stream.getKind().equals("video")){
Log.d("VideoSDK","Video Stream On: onStreamDisabled $stream");
}
}
});

Video Permissions

  • To use the camera in a meeting, 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 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()
}
}
}
}
info

If the app goes to the background, VideoSDK detectes this event based on activity lifecycle and release the camera so that it can be used by other apps. In order to make sure this functionality works properly use VideoSDK.setActivityForLifeCycle(activity) to specify which activity VideoSDK should monitor for lifecycle changes.

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