Change Mode - Android
In a live stream, audience members usually join in RECV_ONLY
mode, meaning they can only view and listen to the hosts. However, if a host invites an audience member to actively participate (e.g., to speak or present), the audience member can switch their mode to SEND_AND_RECV
using the changeMode() method.
This guide explains how to use the changeMode() method and walks through a sample implementation where a host invites an audience member to become a host using PubSub.
changeMode()
​
- The
changeMode()
method from theMeeting
class allows a participant to switch between modes during a live stream—for example, from audience to host.
Example​
- Kotlin
- Java
class LiveStreamActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_live_stream);
// initialize the meeting
liveStream = VideoSDK.initMeeting(... )
...
// join meeting
liveStream!!.join()
// Button to change mode
val changeModeBtn: Button = findViewById(R.id.btnChangeMode)
changeModeBtn.setOnClickListener {
liveStream.changeMode(MeetingMode.SEND_AND_RECV)
}
}
}
public class LiveStreamActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_stream);
// initialize the meeting
Meeting liveStream = VideoSDK.initMeeting( ...);
...
// join meeting
liveStream.join()
// Button to change mode
Button changeModeBtn = findViewById(R.id.btnChangeMode);
changeModeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveStream.changeMode(MeetingMode.SEND_AND_RECV);
}
});
}
}
Implementation Guide​
Step 1 : Create a Pubsub Topic​
- Set up a PubSub topic to send a mode change request from the host to a specific audience member.
- Kotlin
- Java
fun sendInvite(livestream: Meeting, participantId: String) {
val pubSubPublishOptions = PubSubPublishOptions().apply {
isPersist = false
}
livestream.pubSub.publish(
"REQUEST_TO_JOIN_AS_HOST_$participantId", // PubSub topic specific to participant
"SEND_AND_RECV", // message
pubSubPublishOptions
)
}
void sendInvite(Meeting livestream, String participantId) {
PubSubPublishOptions pubSubPublishOptions = new PubSubPublishOptions();
pubSubPublishOptions.setPersist(false);
livestream.pubSub.publish(
"REQUEST_TO_JOIN_AS_HOST_"+ participantId, // PubSub topic specific to participant
"SEND_AND_RECV", // message
pubSubPublishOptions
);
}
Step 2 : Create an Invite Button​
- Add an "Invite on Stage" button for each audience member. When clicked, it publishes a PubSub message with the mode "SEND_AND_RECV" to that participant.
- Kotlin
- Java
// In ParticipantListAdapter.kt, inside showPopup method
if (participant!!.mode == "RECV_ONLY") {
popup.menu.add("Add as a co-host")
popup.setOnMenuItemClickListener { item: MenuItem ->
if (item.toString() == "Add as a co-host") {
sendInvite(liveStream,participantId);
holder.requestedIndicator.visibility = View.VISIBLE
holder.btnParticipantMoreOptions.isEnabled = false
return@setOnMenuItemClickListener true
}
false
}
}
// In ParticipantListAdapter.java, inside showPopup method
if ("RECV_ONLY".equals(participant.getMode())) {
popup.getMenu().add("Add as a co-host");
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if ("Add as a co-host".equals(item.toString())) {
sendInvite(liveStream,participantId);
holder.requestedIndicator.setVisibility(View.VISIBLE);
holder.btnParticipantMoreOptions.setEnabled(false);
return true;
}
return false;
}
});
}
Step 3 : Create a Listener to Change the Mode​
- On the audience side, subscribe to the specific PubSub topic. When a mode request is received, update the participant’s mode using changeMode().
- Kotlin
- Java
// In your class where you define coHostListener
val coHostListener = object : PubSubMessageListener {
override fun onMessage(pubSubMessage: PubSubMessage) {
showCoHostRequestDialog()
}
}
liveStream.pubSub.subscribe(
"REQUEST_TO_JOIN_AS_HOST_${liveStream.localParticipant.id}",
coHostListener
)
// In the showCoHostRequestDialog method
private fun showCoHostRequestDialog() {
// Dialog setup code...
acceptBtn.setOnClickListener {
liveStream.changeMode("SEND_AND_RECV")
}
// Rest of the dialog code...
}
// In your class where you define coHostListener
PubSubMessageListener coHostListener = new PubSubMessageListener () {
@Override
public void onMessage(PubSubMessage pubSubMessage) {
showCoHostRequestDialog();
}
};
liveStream.pubSub.subscribe("REQUEST_TO_JOIN_AS_HOST_" + liveStream.getLocalParticipant().getId(), coHostListener);
// In the showCoHostRequestDialog method
private void showCoHostRequestDialog() {
// Dialog setup code...
acceptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
liveStream.changeMode("SEND_AND_RECV");
}
});
// Rest of the dialog code...
}
API Reference​
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord