Manage On-Screen Participants - Flutter
It is important that only the necessary person are present on the screen when livestream is on. To handle this, we will be using the pin
and unpin
methods of the Participant
object.
To ensure only the hosts/speakers are shown in the grid, you should use the SPOTLIGHT
layout and pin
as the priority when starting the interactive livestream.
Let us first explore two methods that we will be using to manage on-screen participats.
pin()
With these method you can pin any participant's Camera, Screen Share or both. This can be useful to identify the participants based on which you can perform rendering participant grid.
unpin()
With these methods you can unpin any participant's Camera, Screen Share or both. This can be useful to reset pin state of the participant.
Managing On-Screen Participants
- If you want to pin all the hosts/speakers automatically, you can do it by listenting to the
Events.roomJoined
callback andEvents.participantModeChanged
, where you canpin
andunpin
based on the mode.
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() {
...
setupRoomEventListener();
}
@override
Widget build(BuildContext context) {
return YourMeetingWidget();
}
void setupRoomEventListener() {
room.on(Events.roomJoined, () {
//We will pin the participant if mode is conference
if(room.localParticipant.mode == Mode.CONFERENCE){
room.localParticipant.pin();
}
});
room.on(Events.participantModeChanged, (Map<String, dynamic> data){
//We will pin the participant if mode is conference else unpin him
if (data['participantId'] == room.localParticipant.id) {
if (room.localParticipant.mode == Constants.modes.CONFERENCE) {
room.localParticipant.pin();
} else {
room.localParticipant.unpin();
}
}
});
}
}
- When rendering the participant grid on the Speaker side, make sure to show only the participants who are in
CONFERENCE
mode. You can filter the participants as shown in below examples.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class MeetingScreen extends StatefulWidget {
...
}
class _MeetingScreenState extends State<MeetingScreen> {
late Room room;
Map<String, Participant> _speakers = {};
@override
void initState() {
...
updateSpekers();
setupRoomEventListener();
}
@override
Widget build(BuildContext context) {
return YourMeetingWidget();
}
void updateSpeakers(){
Map<String, Participant> speakers = {};
if(room.localParticipant.mode == Mode.CONFERENCE);
speakers.putIfAbsent(
room.localParticipant.id, () => room.localParticipant);
room.participants.values.forEach((participant) {
if (participant.mode == Mode.CONFERENCE) {
speakers.putIfAbsent(participant.id, () => participant);
}
});
setState(()=>_speakers = speakers);
}
void setupRoomEventListener() {
room.on(Events.roomJoined, () {
updateSpeakers();
});
room.on(Events.participantModeChanged, (Map<String, dynamic> data){
updateSpeakers()
});
room.on(Events.participantJoined, (Participant participant) {
updateSpeakers();
});
room.on(Events.participantLeft, (String participantId) {
updateSpeakers();
});
}
}
API Reference
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord