Leave or End Room - Android
Call leave() to take the local participant out of the room. Everyone else stays in it.
Call end() to end the room for everyone. Whether your token is allowed to end the room is decided by the server.
leave()
To leave the room without removing the other participants, call the leave() method of the Room class.
- It also works while the room is reconnecting. Called while a join is still running, it cancels that join.
- It throws
VideoSDKExceptionwith code3035(ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) ifjoin()was never called, and3054(ERROR_ALREADY_IN_REQUESTED_STATE) if it was already left. Neither is reported ononError. - After a failed join, call
leave()too: it releases the room and firesonRoomLeftwithLeaveReason.MANUAL_LEAVE_CALLED. To try again, create a new room withVideoSDK.createRoom().
end()
To end the room for everyone, call the end() method of the Room class.
- It throws
VideoSDKExceptionwith code3035(ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) when you are not in the room,3065(ERROR_ROOM_RECONNECTING) while the room is reconnecting,3057(ERROR_END_ROOM_FAILED) when the server refuses, and3079(ERROR_OPERATION_TIMED_OUT) when the server doesn't answer. If the server refuses, you stay in the room.
Both are suspend functions, so call them from a coroutine and catch VideoSDKException.
Example
btnLeave!!.setOnClickListener { _: View? ->
lifecycleScope.launch {
try {
room!!.leave() // Leaving the room
} catch (e: VideoSDKException) {
// 3035: never joined, 3054: already left
Log.w("VideoSDK", "Leave refused: ${e.code} ${e.name}")
}
}
}
btnEnd!!.setOnClickListener { _: View? ->
lifecycleScope.launch {
try {
room!!.end() // Ending the room
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "End failed: ${e.code} ${e.name}")
}
}
}
lifecycleScope is already cancelled when onDestroy() runs, so a leave() launched there never runs. Leave the room before the screen is destroyed, for example from your leave button as above.
If the screen can close while leave() is still running, call it as withContext(NonCancellable) { room!!.leave() }. Otherwise a cancelled coroutine can stop the leave partway, before the room is left.
Events associated with Leave
The following callbacks are received when a participant leaves the room.
- Local Participant: Will receive
onRoomLeftwith aLeaveReason. Afterleave(), the reason isMANUAL_LEAVE_CALLED. The camera and microphone are already released when it fires. - Remote Participants: Will receive
onParticipantLeftwith theParticipantobject and aLeaveReason.
Events associated with End
The following callbacks are received when a participant ends the room.
- Every participant, you included, receives
onRoomLeftwith aLeaveReason.
private val roomEventListener = object : RoomEventListener() {
// Event to know the room is left
override fun onRoomLeft(reason: LeaveReason) {
Log.d("#VideoSDK", "onRoomLeft() :: reason: ${reason.message}")
}
// Event to know some other participant left
override fun onParticipantLeft(participant: Participant, reason: LeaveReason) {
Log.d("#VideoSDK", "${participant.displayName} left :: reason: ${reason.message}")
}
}
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

