Display Attendees Count - Android
In this guide, we will see how you can display the number of attendees in realtime.
note
Before going forward in this guide, do make sure all the attendees join the live stream with mode as Mode.SIGNALLING_ONLY
- We will get all the participants except the local participant using the
participantsproperty of theRoomclass. From these participants, we will count the ones who have joined asSIGNALLING_ONLYand then display that count.
private fun showViewerCount() {
// count the local participant only when it is an attendee
var viewerCount = if (room!!.localParticipant.mode == Mode.SIGNALLING_ONLY) 1 else 0
val participants = room!!.participants
for (entry: Map.Entry<String, Participant> in participants.entries) {
val participant = entry.value
if (participant.mode == Mode.SIGNALLING_ONLY) {
viewerCount++
}
}
this.viewerCount!!.text = viewerCount.toString()
}
- To keep the count up to date, call
showViewerCount()again when you join the room, and whenever a participant joins, leaves or changes mode.
private val roomEventListener: RoomEventListener = object : RoomEventListener() {
override fun onRoomJoined(switchRoomId: String?) {
showViewerCount()
}
override fun onParticipantJoined(participant: Participant) {
showViewerCount()
}
override fun onParticipantLeft(participant: Participant, reason: LeaveReason) {
showViewerCount()
}
override fun onParticipantModeChanged(participantId: String, mode: Mode) {
showViewerCount()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//...
// add listener to room
room!!.addEventListener(roomEventListener)
}
API Reference
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord

