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 meeting. 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.
enableWebcam()
​
-
By using the
enableWebcam()
function of theMeeting
class, the host can publish their video to to other hosts and audience members. -
You can also pass a customised video track in
enableWebcam()
by using Custom Video Track. -
Video stream of the participant can be accessed from the
onStreamEnabled
event ofParticipantEventListener
.
disableWebcam()
​
- By using
disableWebcam()
function ofMeeting
class, the host can stop publishing their video to to other hosts and audience members.
Example​
- Kotlin
- Java
btnWebcam!!.setOnClickListener {
if (webcamEnabled) {
// Disabling camera
meeting!!.disableWebcam()
} else {
// Enabling camera
meeting!!.enableWebcam()
}
webcamEnabled=!webcamEnabled
}
btnWebcam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (webcamEnabled) {
// Disabling camera
liveStream.disableWebcam();
} else {
// Enabling camera
liveStream.enableWebcam();
}
webcamEnabled=!webcamEnabled;
}
});
To learn, how to render video in the meeting, follow thisdetailed guide .
Events associated with enableWebcam​
- Every Participant—including all the hosts and audience members will receive a callback on
onStreamEnabled()
event of theParticipant
withStream
object.
Events associated with disableWebcam​
- Every Participant—including all the hosts and audience members will receive a callback on
onStreamDisabled()
event of theParticipant
withStream
object.
- Kotlin
- Java
liveStream!!.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");
}
}
});
participant.addEventListener(new ParticipantEventListener() {
//Callback for when the participant starts a stream
@Override
public void 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
public void onStreamDisabled(Stream stream) {
if(stream.getKind().equals("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>
.
<uses-permission android:name="android.permission.CAMERA" />
- You need to set up a permission request that provides this access.
- Kotlin
- Java
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()
}
}
}
}
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class LiveStreamActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
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 boolean checkPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
List<String> permissionList = new ArrayList<String>();
String[] permissions = {};
permissionList.add(Manifest.permission.CAMERA);
ActivityCompat.requestPermissions(
this, permissionList.toArray(permissions),
PERMISSION_REQUEST_CODE
);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
// main logic
} else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
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