Skip to main content
Version: 1.1.x

On / Off Camera - Flutter

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

enableCam()

  • By using enableCam() function of Room object, 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 enableCam() by using Custom Video Track.

  • Video stream of the participant can be accessed from the streams property of Participant object.

disableCam()

  • By using disableCam() function of Room object, 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

import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class MeetingScreen extends StatefulWidget {
...
}

class _MeetingScreenState extends State<MeetingScreen> {
late Room _room;

@override
void initState() {
...
}

@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed:(){
_room.enableCam();
},
child: const Text("Enable Camera"),
),
ElevatedButton(
onPressed:(){
_room.disableCam();
},
child: const Text("Disable Camera"),
),
]
);
}
}
note

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

Getting Participant Camera Status

  • You can get the local participant's media status by going through the stream Map of the Participant object.
  • If stream contains a value where kind is video then the participant's camera is active else it is disabled.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class ParticipantTile extends StatefulWidget {
final Participant participant;
const ParticipantTile({super.key, required this.participant});

@override
State<ParticipantTile> createState() => _ParticipantTileState();
}

class _ParticipantTileState extends State<ParticipantTile> {
Stream? videoStream;

@override
void initState() {
// initial video stream for the participant
//Check if camera stream is already present
widget.participant.streams.forEach((key, Stream stream) {
setState(() {
if (stream.kind == 'video') {
videoStream = stream;
}
});
});

_initStreamListeners();
super.initState();
}

//Change state according to the events received
_initStreamListeners() {
widget.participant.on(Events.streamEnabled, (Stream stream) {
if (stream.kind == 'video') {
setState(() => videoStream = stream);
}
});

widget.participant.on(Events.streamDisabled, (Stream stream) {
if (stream.kind == 'video') {
setState(() => videoStream = null);
}
});
}

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Current Camera status of ${widget.participant.displayName} is ${videoStream !=null ? "Active": "Disable"}")
);
}
}

Events associated with enableCam

Events associated with disableCam

import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class ParticipantTile extends StatefulWidget {
final Participant participant;
...
}

class _ParticipantTileState extends State<ParticipantTile> {

@override
void initState() {
...

_initStreamListeners();
super.initState();
}

//Change state according to the events received
_initStreamListeners() {
widget.participant.on(Events.streamEnabled, (Stream stream) {
if (stream.kind == 'video') {
//Camera Turned On
}
});

widget.participant.on(Events.streamDisabled, (Stream stream) {
if (stream.kind == 'video') {
//Camera Turned Off
}
});
}

@override
Widget build(BuildContext context) {
return YourParticipantWidget();
}
}

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